RewriteCond, RewriteRule, and rewrite logs in apache
Overview
?? The main function of Rewirte is to implement URL redirection. It can be based on the server-level (httpd. conf) and directory-level (. htaccess) methods. To use the rewrite module, you must first install or load the rewrite module.
Remove
"# LoadModule rewrite_module modules/mod_rewrite.so"
# Above;
?? Server-level (httpd. conf) There are two methods, one is in httpd. in conf, RewriteEngine on is used to enable the rewrite function. In addition, the RewriteEngine on is used to enable the rewrite function locally. The following is an example.
?? You must use RewriteEngine on in each virtualhost to enable the rewrite function. Otherwise, rules in virtualhost without RewriteEngine on will not take effect.
?? For directory-based (. htaccess), note that the FollowSymLinks attribute of this directory must be opened and RewriteEngine on must be declared in. htaccess.
RewriteCond
?? 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.
Example 1
RewriteEngine onRewriteCond %{HTTP_USER_AGENT} ^Mozilla/5.0.*RewriteRule index.php index.m.phpRewriteCond %{HTTP_USER_AGENT} ^Lynx.*RewriteRule index.php index.L.phpRewriteRule index.php index.b.php
The purpose of the preceding statement is to use the FF browser to access the index. 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 ):
If ($ _ SERVER ['HTTP _ USER_AGENT '] = 'mozilla/123') {// jump to the index. m. php access} else if ($ _ SERVER ['HTTP _ USER_AGENT '] = 'lynx') {// jump to the index. l. php access} else // jump to the index. b. php access
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 parent page of the url you access is www.test.cn, the access to test. php will be redirected no matter which page you are currently accessing.
Example 3
RewriteCond %{REMOTE_HOST} ^host1.* [OR]RewriteCond %{REMOTE_HOST} ^host2.* [OR]RewriteCond %{REMOTE_HOST} ^host3.*RewriteRule (.*)$ 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.
With so many examples, let's look at his syntax:
RewriteCond TestString CondPattern [flags] # TestString is the matched string, which can be "RewriteRule reverse reference ($ N)" or "RewriteCond reverse reference (% N) "," server variable (% {NAME_OF_VARIABLE}) "," RewriteMap extension ($ {mapname: key | default}) "; # CondPattern is a regular expression applied to the current instance TestString; # TestString will be calculated and matched with CondPattern. # Two results are returned, either match or no-match, and match is used to continue execution.
For specific parameters, see the RewriteCond Chinese manual in apache.
RewriteRule
This command can be used multiple times. Each instruction defines a simple rewrite rule. The order in which these rules are defined is particularly important-the rules take effect one by one at runtime.
Syntax:
RewriteRule Pattern Substitution [flags] # Pattern is a perl-Compatible Regular Expression acting on the current URL. # "Current URL" refers to the value of the URL when the rule takes effect. # It may be different from the requested URL. # because other rules may have been matched before and modified. # Substitution: when the original URL matches Pattern, # is used to replace (or replace) the string. # [Flags] For details, refer to the reference link below.
For more information about the parameters, see
Apache ReWriteRule Parameters
Apache RewriteCond Chinese manual
Give a [flag] Quick query table
The most typical example
RewriteEngine OnRewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule ^(.*)$ index.php?url=$1 [PT,L]
The above rule indicates that the current request is neither a file nor a directory, which overwrites the url to the index. php entry file.
Debug Apache rewrite Rules (rewrite logs)
Are you crazy about debugging apache rewrite rules?
Are you confused when you see N rewrite rules in the project htaccess?
I'm sure there are. The following describes how to debug apache rewrite rules clearly.
1. the apache version is earlier than 2.4.
Mod_rewrite has the log function. The method to enable mod_rewrite is as follows:
Add
RewriteLog "/myfolder/mylogfile.log" RewriteLogLevel 9
Rewriteloglevel 0 indicates disabled, 9 indicates enabling the maximum debug output, and 9 indicates the most detailed rewrite matching information. please paste "/myfolder/mylogfile above. log is replaced with the actual path on your computer. After adding the log, restart apache. when accessing the rewrite page, apache will automatically add records to the log file.
2. apache version 2.4 and later
Those familiar with earlier versions of mod_rewrite will undoubtedly look for the RewriteLog and RewriteLogLevel commands. This feature has been completely replaced by logging configurations for the following new modules.
LogLevel alert rewrite:trace3
Where, the record is equal ~ Trace8. The greater the value, the more information is recorded. You will be able to see this information in the error_log of your apache configuration.
Note:
Apache's RewriteLog command can only be in the conf file, not in the. htaccess file. Using mod_rewrite at the high tracking log level will significantly slow down your Apache HTTP server! Higher log level than trace2 is only used for debugging! Remove configuration items from the production environment to avoid impact on performance.