Introduction to RewriteCond rule parameters in Apache

Source: Internet
Author: User

In Apache, The RewriteCond statement has been a difficult problem for me. I tried to understand it many times without structure. I finally knew what it meant.
Like the if statement in our program, RewriteCond indicates executing the RewriteRule statement next to RewriteCond if one or more conditions are met. This is the original and basic function of RewriteCond, for ease of understanding, let's take a look at several examples.

%{HTTP_USER_AGENT}  ^MozillaRewriteRule  index.php            index.m.%{HTTP_USER_AGENT}  ^Lynx.*.php            index.L..php            index.b.php

 


The preceding statement is used to access the index using the FF browser. the php file will automatically allow you to access the index. m. php file. When you use some mobile terminals to access the file, you will be directed to the index. the actual access to the php file is index. l. if you use another browser to access php, you will be redirected to index. b. php. In terms of image, the above statement is equivalent to the following statement in the Program (taking the PHP statement as an example ):

(['HTTP_USER_AGENT'] == 'Mozilla/5.0' (['HTTP_USER_AGENT'] == 'Lynx'


See example 2:
RewriteCond % {HTTP_REFERER} (www.test.cn)
RewriteRule (. *) $ test. php
The purpose of the preceding statement is that if the host address of the previous page you access is www.test.cn, the access to test. php will be redirected no matter which page you are currently accessing.

See example 3:

RewriteCond %{REMOTE_HOST} ^host1.*%{REMOTE_HOST} ^host2.*%{REMOTE_HOST} ^host3.*.*)$ test.php

 


The purpose of the preceding statement is to jump to test. php if your address is host1, host2, or host3. It can be seen from this that the default AND between the RewriteCond statements is, if you want OR, you need to write it clearly.

Below are some useful Rewrite Rules added to your favorites:


RewriteCond % {REQUEST_FILENAME }! -D // # directly access the directory without RewriteRule if the directory exists

RewriteCond % {REQUEST_URI }! ^. *(/. Css | /. js | /. gif | /. png | /. jpg | /. jpeg) $ // # If files with these suffixes are used, they are directly accessed without Rewrite.

 

 

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.