A detailed introduction to the parameters of Rewritecond rules in Apache

Source: Internet
Author: User
Tags php file
Apache Rewritecond statement for me has been a difficult point, many times trying to understand it, there is no structure, this time I finally figured out what it means

Rewritecond is just like the IF statement in our program, which means that if one or more of the conditions are met, then the Rewriterule statement below Rewritecond is executed, which is the most primitive and basic function of rewritecond, to facilitate understanding, Let's look at a few examples below.

Copy Code code as follows:


Rewriteengine on


Rewritecond%{http_user_agent} ^mozilla//5/.0.*


rewriterule index.php index.m.php


Rewritecond%{http_user_agent} ^lynx.*


rewriterule index.php Index. l.php


rewriterule index.php index.b.php


The function of the above statement is that when you access the index.php file using the FF browser, you will automatically be able to access the file index.m.php, when you are accessing it with some mobile terminals, Will let you actually visit index.php This file access is index.l.php to go, if you are using other browser to visit, will let you jump to index.b.php. In saying the image point, the above statement is equivalent to the following statement in the program (according to the PHP statement as an example):

Copy Code code as follows:


if ($_server[' http_user_agent '] = = ' mozilla/5.0 ')


{


//Jump to index.m.php access


}


else if ($_server[' http_user_agent '] = = ' Lynx ')


{


//Jump to index. L.php's Access


}


Else


//Jump to index.b.php access


in See Example 2:
Rewritecond%{http_referer} (www.test.cn)
Rewriterule (. *) $ test.php
The function of the above statement is that if the host address of the previous page you are visiting is www.test.cn, then whatever page you are currently accessing will jump to the test.php access.
in the example three:

Copy Code code as follows:


Rewritecond%{remote_host} ^host1.* [OR]


Rewritecond%{remote_host} ^host2.* [OR]


Rewritecond%{remote_host} ^host3.*


rewriterule (. *) $ test.php


The effect of the above statement is to skip to test.php if your address is host1 or host2 or HOST3. It can be seen from here that the default is and between the rewritecond statements, and if you want to, explicitly write them.
The following are some useful overriding rules for your own collection:
rewritecond%{request_filename}!-f//If a file exists, direct access to the file. Do not perform the following rewriterule. (Do not overwrite if file or file does not exist)
Rewritecond%{request_filename}!-d//#如果目录存在就直接访问目录不进行RewriteRule
Rewritecond%{request_uri}!^.* (/.css|/. JS|/.GIF|/.PNG|/.JPG|/.JPEG) $//#如果是这些后缀的文件, direct access to the file 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.