Home > Backend Development > PHP Tutorial > Detailed introduction to RewriteCond rule parameters in Apache_PHP tutorial

Detailed introduction to RewriteCond rule parameters in Apache_PHP tutorial

WBOY
Release: 2016-07-20 11:16:39
Original
841 people have browsed it

The RewriteCond statement in Apache has always been a difficult point for me. I have tried to understand it many times, but there is no structure. This time I finally know roughly what it means. ​

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 look at it below Let’s look at a few examples.

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 the 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 terminals to access it, it will allow you to access it. When accessing the index.php file, you actually access index.L.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 to 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, you will jump to access test.php.
Looking at Example 3:

Copy 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.
Here are some useful rewriting rules that you have collected:
RewriteCond %{REQUEST_FILENAME} !-f //If the file exists, access the file directly without performing the following RewriteRule. (If the file does not exist or the file does not exist, rewrite will be performed)
RewriteCond %{REQUEST_FILENAME} !-d //#If the directory exists, access the directory directly without performing RewriteRule
RewriteCond %{REQUEST_URI} !^.*(/.css|/.js|/.gif|/.png|/.jpg|/.jpeg)$ //#If it is a file with these suffixes, access the file directly. No Rewrite

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/372370.htmlTechArticleThe RewriteCond statement in Apache has always been a difficulty for me. I have tried to figure it out many times, but there is no structure. , this time I finally know roughly what it means. RewriteCond is just like us...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template