Rewrite

We *heart* canonical URIs

[Note – I’ve recently come accross a web hosting company that disallows Options +FollowSymLinks. When used their server throws an internal error page – you may need to replace with the more secure (on shared hosting systems anyway) Options +SymLinksIfOwnerMatch ]

Get rid of those pesky Ws.

RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^www.example.co.uk$ [NC]
RewriteRule ^(.*)$ http://example.co.uk/$1 [R=301,L]

Or as you may well prefer – reinstate them

The SEO boys, they say “www“.

RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^example.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.example.co.uk/$1 [R=301,L]

…rewrite continued:

#redirect all .html pages to .php
RewriteBase /
RewriteRule ^(.*).html$ $1.php [R=301]

Redirects

#specific page redirects - PERMANENT
redirect 301 /example-page.html http://example.com/new-name.html

#specific page redirects- TEMPORARY
redirect 302 /example-page.html http://example.com/new-name.html

Spell checker

Thanks to Jens Meiert This little line will allow a single typo in your users’ URIs (guess who just discovered that “URL” is a depreciated term):
CheckSpelling On

Get rid of any instances of “index.html”. And more

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /([^/]+/)index.(html?|php) HTTP/
RewriteRule ^(([^/]+/)
)index.(html?|php)$ http://example.com/$1 [R=301,L]

Add trailing slashes where they may have been missed…

Thanks to Webmaster World

# If final URL-path-part does not contain a period or end with a slash
RewriteCond %{REQUEST_URI} !(.[^/]*|/)$

Redirect to add a trailing slash

RewriteRule (.*) http://www.example.com/$1/ [R=301,L]

more…

Set PDFs to download rather than open in browser

#option for PDFs to download rather than open
AddType application/octet-stream .pdf

Disable directory browsing

# disable directory browsing
Options All -Indexes

Redirect denied directory listings etc to standard error page

# redirect denied directory listings etc to standard error page
ErrorDocument 403 /404-redirect.html
ErrorDocument 404 /404-redirect.html

Ease versioning of CSS files (+)

Will also improve history viewing with GitX

Note: this assumes a naming convention of, for example /css/filename.1234.css. Other dots will break this fragile little fellow.
#Rules for Versioned Static Files
RewriteRule ^(scripts|css)/(.+).(.+).(js|css)$ $1/$2.$4 [L]

Last Updated on