mod_rewrite: A Beginner’s Guide to URL Rewriting

Conditions

But that’s not all! Though RewriteRule gives you an opportunity to have professional URL rewriting, you can make it more customized using conditions.

The format of the conditions is simple:

RewriteCond Something_to_test Condition

Any RewriteCond condition affects the behaviour of the following RewriteRule, which is a little confusing, as RewriteCond won’t be evaluated until the following RewriteRule pattern matches the current URL.

It works like this: mod_rewrite takes all the RewriteRules and starts matching the current URL against each RewriteRule pattern. If there’s a RewriteRule pattern that matches the URL, mod_rewrite checks if there are existing conditions for this RewriteRule, and if the first one returns true. If it does, the proper substitution will occur, but if not, mod_rewrite looks for remaining conditions. When there are no more conditions, the subsequent RewriteRule is checked.

This way you can customize URL rewriting using conditions based on practically everything that’s known during a HTTP transfer in Apache — and a lot more! Basically you can use all of these variables in the Something_to_test string:

  • HTTP header variables:
    HTTP_USER_AGENT, HTTP_REFERER,

    HTTP_COOKIE, HTTP_FORWARDED, HTTP_HOST, HTTP_PROXY_CONNECTION,

    HTTP_ACCEPT

  • Connection & request variables:REMOTE_ADDR, REMOTE_HOST, REMOTE_USER, REMOTE_IDENT, REQUEST_METHOD, SCRIPT_FILENAME, PATH_INFO, QUERY_STRING, AUTH_TYPE
  • Server internal variables:DOCUMENT_ROOT, SERVER_ADMIN, SERVER_NAME, SERVER_ADDR, SERVER_PORT, SERVER_PROTOCOL, SERVER_SOFTWARE
  • System variables:TIME_YEAR, TIME_MON, TIME_DAY, TIME_HOUR, TIME_MIN, TIME_SEC, TIME_WDAY, TIME
  • mod_rewrite special values:API_VERSION, THE_REQUEST, REQUEST_URI, REQUEST_FILENAME, IS_SUBREQ

The condition can be a simple string or a standard regular expression, with additions like:

  • <, >, = simple comparison operators
  • -f if Something_to_test is a file
  • -d if Something_to_test is a directory

As you can see, these are more than enough to specify a condition like this one (taken from the mod_rewrite manual):

  RewriteCond %{HTTP_USER_AGENT} ^Mozilla.*    
  RewriteRule ^/$ /homepage.max.html [L]    

  RewriteCond %{HTTP_USER_AGENT} ^Lynx.*    
  RewriteRule ^/$ /homepage.min.html [L]    

  RewriteRule ^/$ /homepage.std.html [L] 

When a browser requests the index page, 3 things can happen:

  • browser with a Mozilla engine the browser will be served homepage.max.html
  • using Lynx (character-based browser) the homepage.min.html will open
  • if the browser’s name doesn’t contain ‘Mozilla’ nor ‘Lynx’, the standard homepage.std.html file will be sent

You can even disable users from accessing images from outside your server:

  RewriteCond %{HTTP_REFERER} !^$  
  RewriteCond %{HTTP_REFERER} !^http://localhost/.*$ [OR,NC]  
  RewriteCond %{HTTP_REFERER} !^http://mysite.com/.*$ [OR,NC]  
  RewriteCond %{HTTP_REFERER} !^http://www.mysite.com/.*$ [OR,NC]  
  RewriteRule .*.(gif|GIF|jpg|JPG)$ http://mysite/images/bad.gif [L,R] 

But of course, there are endless possibilities, including IP- or time-dependant conditions, etc.

Go to page: 1 | 2 | 3 | 4

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

No Reader comments

Comments on this post are closed.