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 ?



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
  • Thanks 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
    Aug 5 '12 at 7:17
  • Apache 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.
    – ucker
    Aug 5 '12 at 12:42
  • I 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
    Aug 5 '12 at 21:25
  • One 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 example http://yourserver.com/foo?q=bar would redirect to http://serverfault.com/?q=bar.
    – Tometzky
    Aug 5 '12 at 21:41
  • Thanks for the update. RewriteMap redirects dbm=db:/etc/httpd/conf/redirects.db gave error RewriteMap dbm type db is invalid . But when I used RewriteMap 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.
    – ucker
    Aug 6 '12 at 9:13

I'd write a script that generates a new VHOST with just the links from the list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.