There are no comments yet...
Preventing Infinite Redirect Loops with Apache mod_rewrite
June 2, 2009 · No CommentsComments Feed
Have you tried using Apache's mod_rewrite to redirect web traffic to custom URLs only to create an infinite loop? When creating rewrite rules for URLs without a trailing slash, an infinite redirection loop will be your likely result. To prevent this problem, create a preceding rewrite condition which adds the trailing slash to the requested URL. This tutorial will show you how remedy this problem and assumes that you have experience with Apache's mod_rewrite and .htaccess files.
So here's the problem. You wish to rewrite requests for the following URL:
http://www.example.com/sample/title
And redirect the request to the following:
http://www.example.com/sample/title.php
Because there are 30 such pages, you want to write one dynamic rule which covers all scenarios with the use generalized regular expressions.
Your first impulse would probably be to write the following rewrite rule:
RewriteRule ^(.*)$ /sample/$1.php [R=301,NC,L]
The Trailing Slash Problem
The problem with this rule is that it causes a redirection loop in Apache because the URL is missing the trailing slash. If you execute this rule, your browser should give you a Page Load Error because of an infinite loop after iterating through the loop about 20 times. In Firefox, you will see:
Redirect Loop
Firefox has detected that the server is redirecting the request for this address in a way that will never complete. The browser has stopped trying to retrieve the requested item. The site is redirecting the request in a way that will never complete.
* Have you disabled or blocked cookies required by this site?
* NOTE: If accepting the site's cookies does not resolve the problem, it is likely a server configuration issue and not your computer.
The resulting URL can look like this:
http://www.example.com/sample/title.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php
To fix this problem we need to use the following rewrite rule with a trailing slash in the pattern (underlined for emphasis):
RewriteRule ^(.*)/$ /sample/$1.php [R=301,NC,L]
Now, our redirection works perfectly but only for requests which end with a slash such as:
http://www.example.com/sample/title/
Solving The Trailing Slash Problem
To make the redirection universal we will add another rewrite rule which precedes the solution to add the trailing slash when it is missing. The following script adds the trailing slash:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(/)$
RewriteRule (.*)$ /sample/$1/ [R,L]
The first two lines of our script sets conditions for the request. Particularly, we are checking to ensure that the requested file name is neither an existing file name nor an existing directory. The third condition ensures that the request does not already have a trailing slash. Should the request meet all three of these conditions, our rewrite rule takes the request and rewrites it within our subdirectory with the trailing slash. Now that this precedes our solution rewrite rule, all requests are properly rewritten whether they contain a trailing slash or not.
The attached .htaccess file contains the entire script. Place this file within the /sample directory to act upon your specific scenario. For applicable requests, this process will create an additional request for each rewrite, but will serve your redirection needs. If you placed this in the root directory, it will unnecessarily process requests which do not need processing and delay the server response to the visitor.
Conclusion
An infinite redirection loop will be your likely result when creating rewrite rules for URLs without a trailing slash. This tutorial showed you how to remedy this problem by creating a preceding rewrite condition which added the trailing slash to the requested URL.
Tags: WAMP/XAMMP (Apache)
Did you like this post? Then
show your Support
Preventing Infinite Redirect Loops with Apache mod_rewrite
Posted in: WAMP/XAMMP (Apache)