The Apache server power commander part 4
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.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More Web Development Articles
More By Developer Shed
developerWorks - FREE Tools! |
Achieving true agility is a never-ending effort. We will showcase how you can become agile incrementally, a few practices at the time.Which practices should any agile team strive to adopt? What additional practices should you consider based on your needs to scale? Adopting practices are however made much easier with the right tool support. What about if your tools adapt to your practices? We will take a look at how the Jazz technology can be leveraged to make your process change the behavior of your tools. FREE! Go There Now!
|
|
|
|
Learn to enable users to both rate existing animations and to combine existing animations into new snippets. This is the third in a series of three tutorials that chronicle the building of a site that enables collaborative discussion and animation building using Domino and OpenLaszlo. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial version of IBM Rational Business Developer V7.1. Rational Business Developer offers rapid and simplified development of business applications and services through Enterprise Generation Language (EGL) tools, generating Java or mainframe solutions while shielding developers from technical complexities. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial of the Rational Host Access Transformation Services (HATS) Toolkit. The HATS toolkit provides a set of plug-ins for the IBM Rational Software Delivery Platform to help you easily extend your legacy applications. HATS makes your 3270 and 5250 applications available as HTML through the most popular Web browsers, while converting your host screens to a Web look and feel and it also enables you to develop new Web, portal, and rich-client applications. FREE! Go There Now!
|
|
|
|
Asset Reuse is a key strategy for companies looking to create innovative solutions to solve complex software development problems. Searching for, identifying, updating, using and deploying software assets can be a difficult challenge. Listen to this webcast, to learn about strategies and tools that you can leverage for a successful project, including Rational Asset Manager, Rational Software Architect and WebSphere Service Registry and Repository. FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference on December 11 at 1:00 pm ET to get tips on building your own plugins with Rational Method Composer. Get your questions answered! FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to try the IBM SOA Sandbox for connectivity. The SOA Sandbox for connectivity provides a trial environment with the tooling and components to help you explore how to effectively connect your infrastructure and integrate all of the people, processes and information in your company. Use the hosted sandbox to explore SOA techniques that streamline connecting existing IT assets together, as well as learn how to connect them to new business logic. FREE! Go There Now!
|
|
|
|
Join the IBM Watchfire team for an informative discussion on techniques and best practices to proactively manage Web application security and how to effectively build application security testing into the software development lifecycle (SDLC). In this Software Delivery Platform webcast you will learn: How to better understand potential web application security vulnerabilities, best practices and how to effectively integrate application security testing into the software development lifecycle, the importance of detecting and removing software vulnerabilities during application development. FREE! Go There Now!
|
|
|
|
IBM Lotus Notes 8 provides a wide range of developers the ability to provide customized, integrated user interfaces via composite applications and via custom sidebar and toolbar plug-ins. This webcast provides you with tips and techniques to use with out-of-the-box capabilities of Lotus Notes 8, and survey how you can share useful components within your own company and within a larger community. FREE! Go There Now!
|
|
|
|
Viper 2 brings a great value to developer communities including SQL, XML, PHP, Ruby, .NET and Java. You probably already know that DB2 Express-C is free for developers to develop, deploy and distribute. Viper 2 provides a variety of means that help move your application from the development stage to deployment more rapidly. This webcast shows how to best utilize the latest tools available for developing DB2 applications. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |