Hello, please sign in or register
You are here: Home

Apache Rewrites

This is Hot! without being stolen or likely to put you in the burns unit.

mod_rewrite is the module used to re-create a URL when it is received by the server, and means you can take a URL composing of say

www.books.co.uk/author_name/book_title/isbn_number.html

and capture just the isbn_number part and transform it into how its normally written. As a request to

www.books.co.uk/search.php?isbn=isbn_number

instead

 

This function is similar to PHP's preg_match() function but for Apache URL requests

httpd.conf Example Code


# Initiate the rewrite  
RewriteEngine on  
# Define the Rule. Use [L] to not process any folowing rule's  
RewriteRule ^[a-zA-Z\_]*/[a-zA-Z\_]*/([0-9]{9}[0-9|X]{1})\.html$ search.php?isbn=$1 [L]  

Setup

You will need to ensure there is a working apache mod_rewrite installed (it is in most distributions) in linux shell type httpd -l and if mod_rewrite.c appears in the list the module is installed and you can play away. In windows check the

httpd.conf

and ensure LoadModule module_rewrite and AddModule mod_rewrite.c are both uncommented

 

Try It!

In the httpd.conf file. Write the next piece of code at the end or within a VirtualHost


# Change the webroot to the URL for your example.php file 
<directory c:\website\> 
RewriteEngine on   
RewriteRule ^(.*)/(.*)$ example.php?dir=$1&file=$2 [L] 
<directory>

PHP

Create the file example.php


<?php
print_r( $_GET )
?>



Test it by typing in a URL to the location of the directory where example.php is stored and write something like

http://localhost/apache/rewrites

in your browser you should see an array of the values appear as if you had writen the string

http://localhost/example.php?dir=apache&file=rewrites

Now the gates have been open, step through

 

Redirecting based upon Version Number

I have various versions of the same site.
e.g.

  • /var/www/perplexed/version_1/
  • /var/www/perplexed/version_2/

...

Because i am working on them. There are various ways to say to your server. I want version number 2.6!. By having a specific URL
e.g.
Manually rewriting the server path and attaching it to a port. Which is a hassle. As you have to write another virtual host and Listen portnumber

  • http://www.perplexed.co.uk:8080/


Creating a sub directory which redirects all changes to an artificial document root of the script

  • http://www.perplexed.co.uk/2/

httpd.conf
 


<VirtualHost *:80>
 ServerName www.perplexed.co.uk
 DocumentRoot /var/www/perplexed/
 <Directory>
     RewriteEngine On
     RewriteRule ^([0-9]+)/    version_$1/
 </Directory>
</VirtualHost>

The above method will work but cookies need to be set with ports... which is rarely done. And all your URL should include the directory

Another way which i am yet to test is rewriting the subdomain so viewing a version can be done by

  • http://2.version.perplexed.co.uk

The virtual host configuration would look alike

<VirtualHost>
  ServerName version.perplexed.co.uk
  Serveralias *.version.perplexed.co.uk
  <Directory>
      RewriteEngine On
      RewriteBase /
      RewriteCond %{HTTP_HOST} ([0-9]+).version.perplexed.co.uk
      RewriteRule ^(.*)$ ./version_%1/$1
</Directory>
</VirtualHost>

Comments

Title*
Comment

Prove you are not a robot

To prove you are not a robot, please type in the six character code you see in the picture below
Security confirmation codeI can't see this!
Contact
Name*
Email never shown*
Home Page

Author

Andrew Dodson
Since:Feb 2007

Comment | flag

Categories

Bookmark and Share