Apache: What is the best way to handle thousands of permanent redirects?
Solution 1:
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
Solution 2:
I'd write a script that generates a new VHOST with just the links from the list.