Question

I need to permanently redirect about 400 URLs. What's the best way to go about it? The most obvious solution would be to add 400 lines of "Redirect 301 old-url new-url" to .htaccess. Is there a better way?

Example URLs:

50 of these:

/category -> /new-category-name-c/

about 350 of these:

/category/subcategory/product.html -> /slightly-different-product-name.html

Edit: added example

Était-ce utile?

La solution

You may create a PHP(or any other) script which will handle all pages which does not exist. It will detect the old URLs and return 301 redirect to new URL.

If old URLs cannot be parsed by regexp, you will need to create a map, like this:

$urlMap = array(
    '/category1' => '/new-category1',
    '/category2' => '/new-category2'
);

and to look up the new URLs from it. If URL is not in map, then return 404 header or redirect to somewhere else..

You can use the ErrorDocument directive to define custom 404 page:

ErrorDocument 404 /process-urls.php

You can get the path from $_SERVER['REQUEST_URI'].

Update

As suggested by @faa, you can use the RewriteMap apache directive. You need to create a txt file which will map from old URLs to new ones, one mapping per row, space separated:

/category1 /new-category-1
/category2 /new-category-2

Then, in your virtual host file, declare the map:

RewriteMap examplemap txt:/path/to/file/map.txt

And the rewrite rule(may be placed in .htaccess):

RewriteCond ${examplemap:%{REQUEST_URI}|0} !=0
RewriteRule ^(.*) ${examplemap:$1|/not_found.html}

This method is easier to maintain than putting 400 rewrite rules to .htaccess.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top