简体   繁体   中英

Informing the user about a 301 redirect

Consider the following scheme:

site A ------> site B (old URL)  --- 301 redirect ---> site C (new URL)

Here's what happens

The user visits site A. Site A contains a link to site B (old URL). When the user clicks on a link to site B, he is redirected to site C (new URL). Site B issues the redirect to site C.

The problem

When the user finally reaches site C, I'd like to notify him about the redirection.

What I've tried

  • I've tried looking for an HTTP_REFERER header when processing incoming requests in site C, but the referrer URL seems to point to site A. I was expecting it to refer to site B. Am I missing something related to 301 redirects and the implementation of this particular header? I am aware it is set by the browser and may behave unexpectedly, but why can't one expect it to refer to site B (the one which issued the redirect)?
  • Appending a ?redirected=1 query string to the end of the new URL when issuing the redirect sounds like a solution, but it requires a) ensuring canonical URLs are in place to avoid duplicate content and b) messes up the new URL which I'd like to keep as clean as possible inside the address bar;

There is always an option of showing an intermediate redirection page with a predefined timeout until Javascript or meta refresh redirection performs the magic, but I'd like to avoid this approach and show the message in the new website instead to avoid confusing both users and web crawlers as I've read that in-browser redirects are not as reliable as the server-side ones.

What is the general approach to solving this problem?

Update: What about redirecting from site B to, say, example.com/redirect.php?referrer_url=oldurl&redirect_to=newurl ?

The redirect.php would then detect oldurl as the referrer URL, flash the redirect message to the session and issue a 301 redirect to example.com/newurl . Is this a good idea from a SEO perspective?

You could send your own http-header with the redirect for example:

header('RedirectedFrom: www.something.com/ifoo/bar.html');

and use this at target site to recognize redirects

$headers = headers_list();
foreach($headers as $header) {
    list($name,$value) = explode(':',$header);
    if($value == 'RedirectedFrom') {
        // what ever you want to do
        break;
    }
}

I came up with the following solution to the problem.

site B (old URL) is redirected to a particular script at site C, whose job is to handle incoming requests and perform redirects if necessary.

For example, if a user browses site A and clicks on a link to site B, he is then redirected to site C's redirect.php script, which accepts two GET parameters: referrer_url and destination . referrer_url is the old link to site B and destination is a relative path at site C.

The script then makes sure that destination is not an absolute URL (to avoid massive redirects to other sites), appends it to the base URL of site C and redirects the user to an appropriate page. referrer_url is not necessary, but it may be used to detect the domain of the old site B URL (in case there are multiple different domains).

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