简体   繁体   中英

Redirecting a large number of URLs with htaccess or php header

I have undergone a major website overhaul and now have 5,000+ incoming links from search engines and external sites, bookmark services etc that lead to dead pages or 404 errors.

A lot of the pages have corresponding "permalinks" or known replacement hierarchy/URL structure.

I've started to list the main redirects with htaccess or physical files with simply a header location reidrect which is clearly not sustainable!

What would be the best method to list all of the old link addresses and their corresponding new addresses with htaccess, php headers, mysql, sitemap file or is it better to have all broken links and wait for search engines etc to re-index my site?

Are there any implications for having a large number of redirecting files for this temporary period until links are reset?

why not to have this list in any appropriate format - a database or a flat file, and load it in 404 error handler file? And then just search for the REQUEST_URI, and use header(Location:") to redirect

.htaccess

ErrorDocument 404 /404.php

404.php

<?PHP
if ($newurl=db_lookup($_SERVER['REQUEST_URI'])) {
  header("Location: $newurl", 1, 301);
}

What I recently did, was using the Rewrite module triggered by a certain pattern in the incoming URL to request a new address from a folder with a new .htaccess where I grouped all the relevant URLs. Thus I could generate, and in due time maintain, a seperate file by dumping the relevant paramteres from my database.

Allow an example:

In the .htaccess of my site root folder, I have something like this:

RewriteRule ^resource/show/(.*)_([0-9]*)/ http://www.mysite.no/bulkredirect/dam2news/ $2 [R=301,L]

Then, in the folder bulkredirect/dam2news , I put a new .htaccess file with new redirects, eg:

RewriteRule 1119$ http://www.mysite.no/index.php?id=843&tx_ttnews[tt_news]=2594 [R=301,L]

Now, the key here is, of course, to make the two ID's (in this example 1119 and 2594) match, and this I did by a clever concatenated database query. You would also probably need to work out the regexps matching your situation. But the general idea is to have a seperate .htaccess file, both to avoid cluttering of the main file, and to ease later maintenance (might simply repeat the db query and dump it in there).

Header redirects are definitely not the correct answer. If you want to preserve your SEO, you'll want to do 301 redirects. I don't know what the path structure of your site is, but this should be possible with a clever set of mod_rewrite rules. If nothing else, a slew of

Redirect 301 /old-path /new-path

generated with a script should tide you over.

There are absolutely implications for the redirects. If you're doing a meta refresh with a 0 second timeout, Google will accept that as an appropriate substitute for a real 301 redirect, but others might not. If you're not using that particular method, you're losing all your linkjuice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM