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.
Related videos on Youtube
ucker
Updated on September 18, 2022Comments
-
ucker over 1 year
We've a list of 3000 301 redirects. We need assistance on
- What would the best place to put these? It seems putting these 3000 lines inside vhost in httpd.conf would be a mess.
- What are recommended ways to handle thousands of urls?
- How much is it going to affect page loading speed and apache server load ?
Thanks.
-
ucker almost 12 yearsThanks for the answer. I tried it but getting errors: my one line redirects.txt file content (space separated values) mydomain.com/experiment mydomain.com/new_page It gave me parse error on line 'RewriteCond ${redirects:$1} != ""' . Syntax error on line 314 of /usr/local/apache/conf/httpd.conf: RewriteCond: bad flag delimiters I commented that out and then apache started but visiting mydomain.com/experiment (or any other url from mydomain) gave me error "request is being redirected in a way it could not be completed".
-
ucker almost 12 yearsApache is not re-starting with above snippet. Could you please help? I'm not able to see any error related to this in any of the log files.
-
Tometzky almost 12 yearsI said it was untested - I expected that it would need some tweaking. I thought it would be enough, sorry. Httpd did not start as there can not be a space between
!=
and""
- I've corrected my example to a working and tested version now. -
Tometzky almost 12 yearsOne more comment: you'd need to use a different approach if your redirects need to depend on query strings (everything after
?
in URL), but it is also possible. As it is now it would just add query string to redirect, for examplehttp://yourserver.com/foo?q=bar
would redirect tohttp://serverfault.com/?q=bar
. -
ucker almost 12 yearsThanks for the update.
RewriteMap redirects dbm=db:/etc/httpd/conf/redirects.db
gave errorRewriteMap dbm type db is invalid
. But when I usedRewriteMap redirects txt:/etc/httpd/conf/redirects.txt
it worked. I still want to go with hashed way like you explained instead of plain text. But I could not find much about the dbm error. Thanks for your help. -
Tometzky almost 12 years
httxt2dbm
command with no arguments will show you available dbm formats. In my system it was SBDM and DB. Make sure that-f
option passed forhttxt2dbm
while generating a map (for example-f db
) is the same asRewriteMap
map type (for exampledbm=db:
). -
ucker almost 12 yearsIn the end sdbm option worked beautifully.
-
COil over 7 yearsWow it works well, perfect ! (just used it with Apache Apache/2.4.7 (Ubuntu))