|
Module mod_rewrite Tutorial (Part 4)
Next articles: Code Protection Under Cold Fusion - By default, Cold Fusion poweredn sites won't allow for .htaccess functionality which makes it difficult to...
Cloaking Meta Tags - (rt) This short tutorial will cover the cloaking of web page meta tags, which follows a different procedure than the IP delivery and full page cloaking...
Ten Basic Steps for Building a Click-ready Web Site - 1) Assemble a web site development plan that is integrated with your overall marketing processes; the content should be consistent with offline materials...
Module mod_rewrite: Rewriting URLs With Query Strings - The Apache server's module mod_rewrite is typically used to rewrite one URL to turn it into another one...
The Wisenut Framejob - Freshly launched new Google contender Wisenut has met with a good deal of preliminary applause: to date, the media buzz has focused on their lean-and-mean, no-frills...
|
Module mod_rewrite Tutorial (Part 4): Special Directives and Examples by Dirk Brockhausen
In this final part of our tutorial we will take a look at those special directives we haven't covered yet.
These directives cannot be defined on directory level.
This means that you will have to be able to edit the Apache webserver's configuration file (httpd.conf). These permissions will usually only be assigned to users "root" or "admin".
If you wish to log all operations effected by mod_rewrite you can activate logging with the following entries:
RewriteLog /usr/local/apache/logs/mod_rewrite_log RewriteLogLevel 1
These entries are not written into the file ".htaccess" but in "Section 2: 'Main' server configuration" of file "httpd.conf".
All mod_rewrite manipulations will be logged in this file. The log file can have any name you prefer. It can be referenced as an absolute path or relative to ServerRoot.
If you wish to maintain separate log files for individual virtual hosts, you will have to place the pertinent entries in "Section 3: Virtual Hosts", e.g.:
<VirtualHost 192.168.1.1> ServerAdmin webmaster@yourdomain.com DocumentRoot /usr/www/htdocs/yourdomain ServerName yourdomain.com RewriteLog /usr/apache/logs/yourdomain_mod_rewrite_log RewriteLogLevel 1 </VirtualHost>
(Note: If your email reader or browser wraps these lines take care to enter them unwrapped in your file!)
The RewriteLogLevel can be defined within a range of 1 to 8. Normally, 1 will do fine. Higher levels are only required for debugging purposes.
Another directive which is very handy for cloaking purposes are the so-called Rewriting Maps. These are files consisting of key/value pairs, e.g. in the simple format of an ordinary text file:
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
These keys are, as you can see, hostnames or IPs. In this simplistic example the value is always the same, namely "spider".
This directive is entered either in the server section 2 or in the virtual host section 3 in file "httpd.conf":
RewriteMap botBase txt:/www/yourdomain/spiderspy.txt
The Rewriting Map will then be available across your server.
The other directives are entered in file ".htaccess":
RewriteCond ${botBase:%{REMOTE_HOST}} =spider [OR] RewriteCond ${botBase:%{REMOTE_ADDR}} =spider RewriteRule ^(.*).htm$ $1.htm [L] RewriteRule ^.*.htm$ index.html [L]
The conditions will make the system check whether the required access is generated by a spider. To this effect a lookup of file "spiderspy.txt" is triggered.
If the key is found, the value "spider" is returned and the condition is rendered as true.
Next, the first RewriteRule will be executed. This one determines that the called for ".htm" page will be fed to the spider. The variable $1 is equal to the part in parentheses of "^(.*).htm$", i.e. the file name will remain the same.
If the URL is called by a normal human visitor, rule 2 applies: the user will be redirected to page "index.html".
As the ".htm" pages will only be read by spiders, they can be optimized accordingly for the search engines.
You may also use a file in dbm format instead of an ordinary text file. The binary data base format helps accelerate the lookup which is particularly important if you are operating from very large spider lists.
This example given above offers a simple cloaking functionality. All ordinary visitors will always be redirected to the site's "index.html" page and there is no access logging beyond the mod_rewrite logs.
However, it does go to show how you can effectively replace several lines of Perl code with just a few lines of mod_rewrite.
Our last example will illustrate this in some greater detail.
The objective is to present site visitors with your "Picture of the Day". Visitors will click a link, e.g.: < http://www.yourdomain.com/pic.html > which will display a different picture every day.
We will work from these server variables:
TIME_MON TIME_DAY
In file ".htaccess" we will enter the following single code line:
RewriteRule ^pic.html$ pic-%{TIME_MON}-%{TIME_DAY}.html
(Note: If your email reader or browser wraps this line take care to enter it unwrapped in your file!)
The URL called for will be rewritten, e.g. to:
pic-08-28.html pic-08-29.html pic-08-30.html etc.
So all you have to do is upload the pertinent files once, after which you won't need to tend to their daily assignation anymore.
Obviously the time variables can also be used for other periodicities.
-
With this final example our mod_rewrite tutorial has come to its end.
Of course, we have not tackled each and every directive, variable, etc. here.
Rather, we suggest you view this tutorial as a general introduction intended to help you as a start off point towards a more in-depth study of the mod_rewrite module, enabling you to customize it according to your specific requirements.
Dirk Brockhausen is the co-founder and principal of fantomaster.com Ltd. (UK) and fantomaster.com GmbH (Belgium), a company specializing in webmasters software development, industrial-strength cloaking and search engine positioning services. He holds a doctorate in physics and has worked as an SAP consultant and software developer since 1994. He is also Technical Editor of fantomNews, a free newsletter focusing on search engine optimization, available at: < http://fantomaster.com/fantomnews-sub.html > You can contact him at mailto:fntecheditor@fantomaster.com (c) copyright 2000 by fantomaster.com
|
Link to this article, just copy and paste following code:
<a href=http://www.torroid.com/article1186.html>Module mod_rewrite Tutorial (Part 4)</a>
|
Article viewed 733 time(s). Read more: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | |