Wed, 16 Jan 08

Chris 1, mod_rewrite 0 (muhahahaha)

I’ve now spent quite a considerable amount of time trying to get mod_rewrite to do my bidding and I think I might just have cracked it1.

The requirements

The mod_rewrite rules

# Permanently (301) redirect /index.html to / (regex copied from http://www.webmasterworld.com/apache/3155094.htm)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.html\ HTTP/
RewriteRule ^(.*)index\.html$ $1 [R=301,L]

# Permanently (301) redirect /<article>.html to /<article>, ensuring that we don't try to redirect index.html to index
RewriteCond %{THE_REQUEST} .+/[0-9]{4}/[0-9]{2}/[0-9]{2}/.+\.html\ HTTP/
RewriteCond %{THE_REQUEST} !index\.html\ HTTP/
RewriteRule ^(.*)\.html$ $1 [R=301,L]

# If we receive a request for an article without a .html extension then let's rewrite internally to render the file with the .html extension
RewriteCond %{REQUEST_URI} .+/[0-9]{4}/[0-9]{2}/[0-9]{2}/.+$
RewriteCond %{REQUEST_URI} !\.html$
RewriteRule ^(.*)$ $1.html [L]

# Permanently (301) remove www.
RewriteCond %{HTTP_HOST} ^www.the-local-paper.co.uk$ [NC]
RewriteRule ^(.*)$ http://the-local-paper.co.uk/$1 [R=301,L]

# Block access to the .svn directories
<DirectoryMatch "^/.*/\.svn/">
  ErrorDocument 403 /404.html
  Order allow,deny
  Deny from all
  Satisfy All
</DirectoryMatch>

I figured that there’s a chance some of these rules might come in useful to someone else.

1 This has all been for the local paper idea I mentioned a while back. I’ve actually started to get some content up on the-local-paper.co.uk if you want to play along and see how it’s shaping up..