What's the best way to redirect 1,000,000 unique URLs to another 1,000,000 unique URLs?

These pages are all combinations of multiple search filters and queries. We're migrating the site to cleaner URLs and want to map old pages to new ones so that we do not lose visitors or send them to 404s.

I believe a million line .htaccess will slow down the site pretty significantly. Is there a better way that would maintain PageRank but not slow down the site? Maybe something I could put on each page individually?

  • Are the pages served via a script? Redirects can be triggered from php (or other scripts) without .htaccess. Jul 16 '13 at 20:19
  • 3
    Are you switching from non-sef to sef? You can use conditional rewrites that cover most/some/lots etc. Give an example of the URL in comment plz. Jul 16 '13 at 21:00
  1. Store your old and new redirect paths in a database.

  2. Redirect 404 errors to a special 404 page. With Apache and PHP, you could do this with the ErrorDocument directive in your .htaccess:

    ErrorDocument 404 /404.php

  3. In the 404.php script, get the referring page with $_SERVER['REQUEST_URI'], look up the old URL path in your database, and serve a 301 redirect header to the new location if there is one, otherwise show your regular 404 page.

    To issue a 301 header with PHP, you'd useheader("Location: $new_url", true, 301);

The advantage of this over Apache mod_rewrite redirects is that you don't need to process the ruleset for every request – only those that trigger a 404.

The disadvantage is that you have to create a system to manage the rewrite rules in a database, but that's probably worth doing if you have a million rules in the first place.

  • 3
    +1 However, in the case of a custom error document, it's not the HTTP_REFERER you need to check. The HTTP_REFERER, if set at all, will contain the referring page before the URL that triggered the 404 (the ErrorDocument is served instead of the requested page, the browser is not redirected to it). You need to check either REQUEST_URI or REDIRECT_URL (that omits the querystring).
    – MrWhite
    Jul 17 '13 at 19:52
  • 2
    +1. Of course, if you can write a program to generate the new URLs from the old ones, then you won't even need to store them in a database. (Also, if you do use a database, creating an index on the old URL column is highly recommended...) Aug 22 '13 at 15:30

You can put a meta redirect right into the HTML markup of each page. For example, put the following somewhere between <head> and </head>:

<META http-equiv="refresh" content="0;URL=/path/to/redirect/to"> 

Or to be more SEO friendly, add a header. For example in PHP:

header('Location: /path/to/redirect/to')

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.