Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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?

share|improve this question
    
Are the pages served via a script? Redirects can be triggered from php (or other scripts) without .htaccess. –  Stephen Ostermiller Jul 16 '13 at 20:19
2  
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. –  bybe Jul 16 '13 at 21:00
add comment

2 Answers

  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.

share|improve this answer
2  
+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). –  w3d Jul 17 '13 at 19:52
    
@w3d Great catch. Thanks. –  Nick Jul 18 '13 at 9:27
1  
+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...) –  Ilmari Karonen Aug 22 '13 at 15:30
add comment

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')
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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