Using RewriteMap for many URL redirects.

Migrating platforms can lead to broken links on your website.  If you’ve cultivated links from other sites or if search engines send a lot of visitors your way, you’ll want to redirect users from the old links to the new.  Many content management systems have facilities for manually maintaining redirects, but you can also do this with Apache.  The big benefit of using Apache is that such visitors won’t hit your CMS until they get to the correct page.

One way to redirect users is through mod_rewrite and clever use of its Rewrite Rules. If you have a lot links to redirect, you don’t want to create one rule for each link, since its essentially the same pattern for each link.  This is where the rewrite modules RewriteMap directive comes in handy.

In your VirtualHost or Server sections, add something like the following, making sure to change the rewrite rule regular expression to match your own situation.


RewriteEngine On
RewriteMap redirect_map txt:/path/to/mysite/redirect_map.txt
RewriteRule ^/news/detail/(.*) ${redirect_map:$1} [R=permanent,L]

Then, in the redirect_map.txt file, you can put the old link and new link one per line and separated by one or more spaces or tabs.


1877-avoiding_frustration_with_php_sessions     /avoiding-frustration-with-php-sessions
1877-avoiding_frustration_with_php_session      /avoiding-frustration-with-php-sessions
2062-howto_use_virtualbox_to_setup_an_internet_explorer_testing_machine /howto-use-virtualbox-to-setup-an-internet-explorer-testing-machine
2067-peter_wilts_pillars_of_management          /peter-wilts-pillars-of-management