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:
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):
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:
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