Apache rewrite Regular Expression basics, apacherewrite

Source: Internet
Author: User

Apache rewrite Regular Expression basics, apacherewrite

I used rewrite several times, but I didn't use many times. I forgot to use it each time. I had to find a bunch of items on the Internet and summarize them to avoid forgetting it later.

================================== Separator ========================== ===

Note the following:

1. Load the rewrite module, LoadModule rewrite_module modules/mod_rewrite.so

2. If it is configured in vhost, open RewriteEngine on in vhost (the htaccess file configuration method has not been used)


Used configurations:

Scenario 1: When the domain name is aaa. bbb, jump to the domain name aaa. ccc. bbb

RewriteEngine on

RewriteCond % {HTTP_HOST} ^ aaa \. bbb $ [NC]
RewriteRule ^/(. *) $ http://aaa.ccc.bbb/3691 [R = 301, L, NC]

% {HTTP_HOST} matches the domain name. ^ indicates that the match starts, and $ indicates that the match ends. They are the left and right sides of the match string. They can appear at different times ,\. escape, match only ., otherwise it will become any single character.

NC (no case) is case insensitive,

. Is any single character ,. * Any number of arbitrary characters, () is a sub-string, so ^ /(. *) $ indicates any path after the domain name. Here we guess we can omit $, here ., \ escape is not used because this part is not a regular expression. $1 matches the content in the first (). R = 301 indicates permanent jump,


Scenario 2: transfer the/testt/request to/image/, but the/testt/xxxx request does not jump.

RewriteEngine on
RewriteRule ^/testt/$ http: // localhost: 8000/image/[R = 301, L, NC]

For query string operations, refer to the official documentation below

Modifying the Query String

By default, the query string is passed through unchanged. you can, however, create URLs in the substitution string containing a query string part. simply use a question mark inside the substitution string to indicate that the following text shocould be re-injected into the query string. when you want to erase an existing query string, end the substitution string with just a question mark. to combine new and old query strings, use[QSA]Flag.


================================== Separator ========================== ===

References:

The meaning of various characters in regular expressions http://www.cnblogs.com/afarmer/archive/2011/08/29/2158860.html

Apache rewrite rule usage instructions http://www.jb51.net/article/48780.htm

13 practical Apache Rewrite rule http://www.jb51.net/article/47907.htm




Apache configuration Rewrite Rule Regular Expression $1 $2 when the number of parameters exceeds 9, $10 cannot correspond to 10th Regular Expressions. What should I do?

$10: $ {10}. If you use $10, the program cannot identify whether 0 or $10 is followed by $1. Of course, it is processed as $1 as a regular method.
 
Regular Expression apache2 mod_rewrite

RewriteRule ^/(. *) www.aaa.com: 54321/$1 [P]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.