Redirection recipes

Some 3,000 Apache redirection rules are getting scrapped by a client (not a bad thing), and I wanted to make sure I had a note before that happened. All 3,000 rules are all variations on the following few types of redirection rule.

Redirecting a branching tree of URLs using mod_alias, a many to many redirection, this is a nice simple one liner. Example redirects:

  • http://example.com/whatever/stuff/ redirects to http://whatever.example.com/stuff/
  • http://example.com/whatever/stuff/image.jpg redirects to http://whatever.example.com/stuff/image.jpg
Redirect permanent /2010/ http://www.example.com/news/2010/

A redirect to outside the current domain which stops further rewrite rule processing, a many to many redirection, using mod_rewrite here rather than mod_alias “Redirect permanent” it allows us to stop executing further rewrite rules if we match one of these; e.g. if there are rewrite rules further down which we specifically need to avoid.

This rule redirects everything under http://example.com/whatever/ to http://whatever.example.com/, same example redirects as above.

RewriteCond %{REQUEST_URI} ^/whatever(.*)? RewriteRule ^whatever http://whatever.example.com%1 [R=permanent,L]

Collapse a branching tree of URLs into one URL using mod_rewrite, here’s some examples:

  • http://example.com/whatever/ redirects to http://example.com/things/
  • http://example.com/whatever/stuff/ redirects to http://example.com/things/
  • http://example.com/whatever/stuff/image.jpg redirects to http://example.com/things/
RewriteRule ^whatever.* http://www.example.com/things/ [R=permanent,L]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.