Apache: What is the best way to handle thousands of permanent redirects?

7,481

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.

Share:
7,481

Related videos on Youtube

ucker
Author by

ucker

Updated on September 18, 2022

Comments

  • ucker
    ucker over 1 year

    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.

  • ucker
    ucker almost 12 years
    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
    ucker almost 12 years
    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.
  • Tometzky
    Tometzky almost 12 years
    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
    Tometzky almost 12 years
    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.
  • ucker
    ucker almost 12 years
    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.
  • Tometzky
    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 for httxt2dbm while generating a map (for example -f db) is the same as RewriteMap map type (for example dbm=db:).
  • ucker
    ucker almost 12 years
    In the end sdbm option worked beautifully.
  • COil
    COil over 7 years
    Wow it works well, perfect ! (just used it with Apache Apache/2.4.7 (Ubuntu))