Apache mod_rewrite, RewriteCond, {HTTP_HOST} _ PHP Tutorial

Source: Internet
Author: User
Tags subdomain subdomain name
Apache mod_rewrite, RewriteCond, {HTTP_HOST }. 1. Add www Mark % {HTTP_HOST} to the subdomain name }! ^ Www ..? Http: This rule captures the % 1 variable of the second-level domain name. if it does not start with www, add www, the previous domain name, and {REQUEST _ 1. Add www tags to the subdomain name.

% {HTTP_HOST }! ^ Www \..? Http: This rule captures the % 1 variable of the second-level domain name. if it does not start with www, www will be added, and the previous domain name and {REQUEST_URI} will follow it.

2. remove the www mark from the domain name

.? http:

3. remove the www tag, but save the sub-domain name

.? Http: here, after matching the 1% variable, the sub-domain name will be crawled in % 2 (internal atom), and what we need is this % 1 variable.



4. prevent Image leeching

% {HTTP_REFERER }! ^ % {HTTP_REFERER }! ^ Http: RewriteRule \. (gif | jpg | png) $-| jpg |%{ HTTP_REFERER }! ^ % {HTTP_REFERER }! ^ Http: RewriteRule \. (gif | jpg | png) $ your image address [R = 301, % {HTTP_REFERER }! ^ Http: RewriteRule \. (gif | jpg | png) $-[F,

5. redirect to 404 page if the file does not exist

% {REQUEST_FILENAME }! -% {REQUEST_FILENAME }! -.? /404.-f matches the existing file name, and-d matches the existing path name. Before you perform 404 redirection, this code determines whether your file name and path name exist. You can also add one on the 404 page? Url = ^ /? (. *) $/404.php? Url = $1



6. rename the Directory

^ /? Old_directory/([a-z/.] +) $ new_directory/$1 [R = 301 ,. "(note that not all characters are included in front of the escape character) to match the suffix of the file.


7.convert the suffix of .html to. php.

^/?([a-z/]+)\.html$ $1.=301,L]。 


8. create a link without a file suffix

^ /? ([A-z] +) $1.% {REQUEST_FILENAME}. php-^ /? ([A-zA-Z0-9] +) $ 20.1.20.20.request_filename=.html-^ /? ([A-zA-Z0-9] +) $1 .. php is the suffix and this rule will be executed.

9. check the specific parameters in the query variable.

%{QUERY_STRING} !uniquekey=^/?script_that_requires_uniquekey\.php$ other_script.php [QSA,


10. delete query variables

. Assign a new query parameter (you can use [QSA,. add "?" after the file name (Such as index. php ?). Symbol "?" It is not displayed in the address bar of the browser.

11. display the current URI in a new format

% {QUERY_STRING }! % {QUERY_STRING} id = (-a-zA-Z0-9 _ +] + ^ /? Index \. php $ % 1? [R = 301, ^ /? ([-A-zA-Z0-9 _ +] +) $ index. php? Marker & id = $1: The second match, marker is matched, so the first rule is ignored. "character will appear in http: Note that this solution requires some Apache extension functions, so if your website is placed on a shared host, there will be many obstacles.


12. enable security services

% {REQUEST_URI} ^ secure_page \. % {HTTPS }! ^ /? (Secure_page \. php) $ https: the above rule tests whether the value of {REQUEST_URI} is equal to our security page code, and {HTTPS} is not equal to on. If both conditions are met, the request will be redirected to the security service URI. % {REQUEST_URI} ^ secure_page \. % {SERVER_PORT }! ^ 443 ^ /? (Secure_page \. php) $ https:

13. enforce security services on a specific page

%{SERVER_PORT} !^443^/?(page1|page2|page3|page4|page5)$ https:%{ SERVER_PORT } ^443!^/?(page6|page7|page8|page9):
                                                      

                                                                          

The dig domain name is marked with www % {HTTP_HOST }! ^ Www \..? Http: This rule captures the % 1 variable of the second-level domain name. if it does not start with www, add www, the previous domain name and {REQUEST _...

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.