mod_rewriting an entire site

Using mod_rewrite to redirect all pages to one central PHP page’.

On my site, I decided to use an all-index structure, as that’s how I prefer to do things – it means that the scripting language is more hidden from the end user than if you linked to pages such as “something-bizarre.jsp” and means that if the scripting language used to create the pages was changed the names of the pages wouldn’t have to be.
In using mod_rewrite to modify an entire website, the following points needed to be addressed:

  • Images and CSS files should not be rewritten
  • Since the only subdomain used by the site is ‘www’, if the user does not enter it then it should be added automatically and visibly for them.
  • All versions of a webpage should be automatically and visibly rewritten to a single URL. i.e. ‘www.example.com/somepage/’, ‘example.com/somepage/’, ‘www.example.com/somepage’ and ‘example.com/somepage’ should all resolve to ‘www.example.com/somepage/’
  • Once all visible rewriting has been completed, the URL should be invisibly redirected to a master page which is able to interpret the URL which the user requested and serve up the correct content.

The following is what I came up with. Please refer to “mod_rewrite, a beginner’s guide (with examples)” if you need any extra pointers as to what anything means.

###################################################
# Turn the RewriteEngine on.                      #
###################################################

RewriteEngine on

###################################################
# Add a leading www to domain if one is missing.  #
###################################################
# If this rule is used, the rewriting stops here  #
# and then restarts from the beginning with the   #
# new URL                                         #
###################################################

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

###################################################
# Do not process images or CSS files further      #
###################################################
# No more processing occurs if this rule is       #
# successful                                      #
###################################################

RewriteRule \.(css|jpe?g|gif|png)$ - [L]

###################################################
# Add a trailing slash if needed                  #
###################################################
# If this rule is used, the rewriting stops here  #
# and then restarts from the beginning with the   #
# new URL                                         #
###################################################

RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]

###################################################
# Rewrite web pages to one master page            #
###################################################
# /somepage/            => master.php             #
#                            ?page=somepage       #
# /somesection/somepage => master.php             #
#                            ?section=somesection #
#                            &page=somepage       #
# /somesection/somesub/somepage/                  #
#                       => master.php             #
#                            ?section=somesection #
#                            &subsection=somesub  #
#                            &page=somepage       #
###################################################
# Variables are accessed in PHP using             #
# $_GET['section'], $_GET['subsection'] and       #
# $_GET['page']                                   #
###################################################
# No more processing occurs if any of these rules #
# are successful                                  #
###################################################

RewriteRule ^([^/\.]+)/?$ /master.php?page=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ /master.php?section=$1&page=$2 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /master.php?section=$1&subsection=$2&page=$3 [L]

Source: http://www.workingwith.me.uk/blog/software/open_source/apache/mod_rewriting_an_entire_site