Basics of Apache rewrite Regular Expressions

Source: Internet
Author: User

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]


================================== 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



Basics of Apache rewrite Regular Expressions

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.