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.