This page has been robot translated, sorry for typos if any. Original content here.

Module mod_rewrite ч.4

In this publication we will touch on those directives that did not have time to cover in the previous parts. These directives can not be determined at the directory level. This means that you must have access to the Apache web server configuration file (httpd.conf).

Usually such access is available to users "root" or the server administrator.

If you want to log all the operations performed with mod_rewrite, you can activate it with the following entry: RewriteLog /usr/local/apache/logs/mod_rewrite_log
RewriteLogLevel 1
RewriteLog /usr/local/apache/logs/mod_rewrite_log
RewriteLogLevel 1

This line should be entered in "Section 2: Main Server Configuration" in the httpd.conf file, not in the .htaccess file!

All manipulations made by mod_rewrite will be written to this file. The name of the log file can be anything. You can specify an absolute or relative (relative to ServerRoot) path to the file.

If you want to keep different log files for different virtual hosts, then you need to enter changes to "Section 3: Virtual servers", for example: ServerAdmin DocumentRoot /usr/www/htdocs/yourdomain ServerName RewriteLog /usr/apache/logs/yourdomain_mod_rewrite_log
RewriteLogLevel 1
ServerAdmin DocumentRoot /usr/www/htdocs/yourdomain ServerName RewriteLog /usr/apache/logs/yourdomain_mod_rewrite_log
RewriteLogLevel 1

RewriteLogLevel can be defined within the range from 1 to 8. Usually the first level is sufficient. Higher levels are used for debanging.

Another directive, which is very convenient for cloaking purposes, is the so-called rewriting card . These are files containing key / value pairs, usually in the format of a text file: spider spider spider spider spider spider spider spider

The keys, as you can see, are host names or IP addresses. In this simple example, the value is always one - "spider". Naturally, in a real file, the values ​​will be different. This directive can be written to the second ("Main server configuration") or the third ("Virtual servers") section of the httpd.conf file: RewriteMap botBase txt:/www/yourdomain/spiderspy.txt

A "rewrite map" will have an effect on the entire server.

Also, the following is written to the .htaccess file: RewriteCond ${botBase:%{REMOTE_HOST}} =spider [OR] RewriteCond ${botBase:%{REMOTE_ADDR}} =spider RewriteRule ^(.*).htm$ $1.htm [L] RewriteRule ^.*.htm$ index.html [L]

These conditions will perform a system check: whether the query is made by the search engine. For this purpose, the spiderspy.txt file is searched. If the key is found, the value "spider" will be returned, and the "condition" will be true.

Then the first RewriteRule is executed. This means that the requested ".htm" page will be given to the search engine. The variable $ 1 is equal to the part in the parentheses "^ (. *). Htm $", that is, the file name will remain the same.

If the URL is called by an ordinary visitor, then the second "rule" is applied: the user will be redirected to the page "index.html".

Since ".htm" pages will be read only by "spiders", they can be optimized accordingly for search engines. You can also use the file in the "dbm" format instead of the regular text file. Binary data format allows you to speed up the search, which is especially important if you work with very large lists of search engines. The example given above offers simple cloaking functionality. All regular visitors will always be redirected to the "index.html" page and no log files will be logged outside the logs of mod_rewrite.

You can replace several lines of code php (perl, etc.) in your applications, using just one or two lines of mod_rewrite. The latter example will illustrate this in more detail.

The goal is to show visitors a "photo of the day". A visitor who clicks on the link will see the best photo or picture of the day, and so every day. We will work with server variables:


Put one single line in the .htaccess file: RewriteRule ^pic.html$ pic-%{TIME_MON}-%{TIME_DAY}.html

The requested URL will be overwritten, for example:

and so on.

Now, all you have to do is once to upload files with the proper names and forget about the daily update of the link. Time variables can also be used for other periodicity.

This was the last example in a series of publications about the remarkable module mod_rewrite. Naturally, it was impossible to touch all the nuances, directives, variables, etc. in this publication, the goal was different - to give a general idea and understanding of the basics, and so to say "to bring things to the table."