I have about 6000 URLs that need to be redirected after changing the CMS. All those URLs are stored in a DB table together with an ID that allows me to tell what is being referenced. Using this table I could generate one huge .htaccess with a lot of Rewrite rules but I suspect that is very inefficient. What's the best way to go? As far as I can tell there are two options:

  • Using a RewriteMap where the key is the old URL (it contains no ID, just text, so it has to be the whole thing). I wonder if having 100-150 char long keys is gonna work well.
  • Generating a DB table that exactly matches every old/new URL pair and calling some small script to make the redirect.

(Could be summed up as: Is a hash map stored in the filesystem more efficient than a indexed DB table?)

Second part of the question. The new URLs contain the ID of the page being invoked. Something like


If instead I call


I get a 301 Redirect to the correct URL. Should I bother to generate the exact new URL or is it ok to concatenate two redirects? This means either storing just an ID or the whole new URL.

1 Answer 1


Creating a huge .htaccess can have a serious performance impact on your system as it is read linearly for all requests, at least until a rule with the L attribute (Last Rule) is matched.

The way I did something similar (about 3000 redirects) is to put a custom error page in the .htaccess. From memory the syntax is something like this:

ErrorDocument 404 404.php

Then in 404.php, implement the logic using a MySQL database, searching for the new URL using the requested URL. If it is found, redirect the request using the PHP location header, if not, then display a custom error page.

Unlike simply issuing the 404 redirect in the .htaccess, the 404.php file can keep track of requests and keep stats on the redirects being returned.

  • Do you have any data on performance? i.e. How much slower than a "normal" request is it?
    – Omar Kohl
    Jul 24, 2011 at 19:40
  • It is easy to measure but it will completely depend on your server configuration, file-system cache including other activities happening on the server (since they compete with the FS cache). Just fill a .htaccess of a few thousand rules (they do not even have to apply) and see if you can measure a change in your server. With the 404 approach, the lookup only happens when something is not found.
    – Itai
    Jul 24, 2011 at 21:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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