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

Module mod_rewrite part 4

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

Typically, such users have access "root" or server administrator.

If you want to keep logs of all operations performed using 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 needs to be entered in "Section 2: Configuration of the main server" in the httpd.conf file, not in .htaccess!

All manipulations performed by mod_rewrite will be written to this file. Log file name can be any. You can specify an absolute or relative (relative to ServerRoot) file path.

If you want to keep different log files for different virtual hosts, then you need to make changes to “Section 3: Virtual Servers”, for example: ServerAdmin webmaster@yourdomain.com DocumentRoot /usr/www/htdocs/yourdomain ServerName yourdomain.com RewriteLog /usr/apache/logs/yourdomain_mod_rewrite_log
RewriteLogLevel 1
ServerAdmin webmaster@yourdomain.com DocumentRoot /usr/www/htdocs/yourdomain ServerName yourdomain.com RewriteLog /usr/apache/logs/yourdomain_mod_rewrite_log
RewriteLogLevel 1

RewriteLogLevel can be defined within the range from 1 to 8. The first level is usually enough. Higher levels are used for debugging.

Another directive that is very convenient for cloaking purposes is the so-called rewriting card . These are files containing key / value pairs, usually in text file format: cde2c920.infoseek.com spider 205.226.201.32 spider cde2c923.infoseek.com spider 205.226.201.35 spider cde2c981.infoseek.com spider 205.226.201.129 spider cde2cb23.infoseek.com spider 205.226.203.35 spider

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

The "rewrite card" 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 produce a system check: whether the request was made by a search engine. For this purpose, the search is performed in the file spiderspy.txt. If the key is found, the “spider” value 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 a regular visitor, then the second “rule” is applied: the user will be redirected to the “index.html” page.

Since the “.htm” pages will be read only by spiders, they can be optimized accordingly for the search engines. You can also use a dbm file instead of a plain 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 above offers simple cloaking functionality. All regular visitors will always be redirected to the “index.html” page and no log files will be kept outside mod_rewrite logs.

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

The goal is to show visitors the “photo of the day”. A visitor who clicks the link http://yoursite.com/pic.html will see the best photo or picture of the day, and so every day. We will work with server variables:

TIME_MON
TIME_DAY

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:

pic-08-28.html
pic-08-29.html
pic-08-30.html
and so on.

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

This was the last example in a series of publications about the wonderful mod_rewrite module. Naturally it was impossible to touch on all the nuances, directives, variables, etc. in this publication, the purpose was different - to give a general idea and understanding of the basics, and so to say “get in the know”