Apache – Best Way to Handle Thousands of Permanent Redirects

301-redirectapache-2.2httpd.conf

We've a list of 3000 301 redirects. We need assistance on

  1. What would the best place to put these? It seems putting these 3000 lines inside vhost in httpd.conf would be a mess.
  2. What are recommended ways to handle thousands of urls?
  3. How much is it going to affect page loading speed and apache server load ?

Thanks.

Best Answer

You can use Include directive in httpd.conf to be able to maintain redirects in another file. But it would not be very efficient, as every request would need to be checked against a lot of regular expressions. Also a server restart would be required after every change in the file.

A better way for so many redirects would be to use RewriteMap directive of type dbm to declare a map from URI's to redirects. This way it will be efficient, as dbm lookups are very fast, and after a change in the map you would not need to restart a server, as httpd checks for map file modification time.

A rewrite rules would look like this (tested on my Fedora 16 computer):

RewriteEngine On
RewriteMap redirects dbm=db:/etc/httpd/conf/redirects.db
RewriteCond ${redirects:$1} !=""
RewriteRule ^(.*)$ ${redirects:$1} [redirect=permanent,last]

And dbm map would be created from text map /etc/httpd/conf/redirects.txt looking like this:

/foo http://serverfault.com/
/bar/lorem/ipsum/ http://stackoverflow.com/

using a command

httxt2dbm -f db -i /etc/httpd/conf/redirects.txt -o /etc/httpd/conf/redirects.db