RewriteCond is just like the if statement in our program. It means that if one or several conditions are met, the RewriteRule statement immediately below RewriteCond will be executed. This is the most original and basic function of RewriteCond. To facilitate understanding, let’s take a look at a few example.
Copy code The code is 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 use FF browser to access the index.php file, it will automatically allow you to access the index.m.php file. When you use some mobile When you access the file through the terminal, you will actually access index.L.php when accessing the file index.php. If you access it using another browser, you will be redirected to index.b.php. Speaking more vividly, the above statement is equivalent to the following statement
in the program (take the PHP statement as an example):
Copy code The code is as follows:
if($_SERVER['HTTP_USER_AGENT'] == 'Mozilla/5.0')
{
//Jump to access to index.m.php
}
else if($_SERVER['HTTP_USER_AGENT'] == 'Lynx')
{
//Jump to access index.L.php
}
else
//Jump to access to index.b.php
Looking at 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 visited is www.test.cn, no matter which page you are currently visiting, it will jump Go to access to test.php.
Looking at Example 3:
Copy the code The code is as follows:
RewriteCond %{REMOTE_HOST } ^host1.* [OR]
RewriteCond %{REMOTE_HOST} ^host2.* [OR]
RewriteCond %{REMOTE_HOST} ^host3.*
RewriteRule (.*)$ test.php
The function of the above statement is that if your address is host1 or host2 or host3, it will jump to test.php. It can be seen from here that the default between RewriteCond statements is AND. If you want OR, you must write it explicitly.
The following are some useful rewrite rules in my collection:
RewriteCond %{REQUEST_FILENAME} !-f //If the file exists, access the file directly, no Carry out the following RewriteRule. (Perform rewrite if the file or file does not exist)
RewriteCond %{REQUEST_FILENAME} !-d //#If the directory exists, directly access the directory without performing RewriteRule
RewriteCond %{REQUEST_URI}! ^.*(/.css|/.js|/.gif|/.png|/.jpg|/.jpeg)$ //#If it is a file with these suffixes, access the file directly without Rewrite
http://www.bkjia.com/PHPjc/327986.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327986.htmlTechArticleRewriteCond is just like the if statement in our program, which means that if one or several conditions are met, RewriteCond will be executed. The RewriteRule statement immediately below is the original and basic RewriteCond...