I am applying a slug system for this site right now. I intend to redirect invalid slugs towards the correct on that's saved within the database. E.g.

http://example.com/11/wrong-slug

Hit db, see if 11's slug is wrong-slug otherwise do 301 redirect

http://example.com/11/right-slug

Identify 301 and inform user they adopted an invalid link

Can you really identify the 301 redirect ideally using PHP in order to request the consumer to update there bookmark etc.

Thanks, Jamie.

Alternatively you can append a GET parameter for your url (if you do not mind that), and appearance for this inside your PHP script. Something similar to:

http://example.com/11/right-slug?corrected-from=http://example.com/11/wrong-slug

On a single note you should use the session or snacks, however, you must be certain to take them off after recognition.

Several solutions spring to mind:

  1. You could attempt with get_headers() ah no, it dispatches a request towards the URL, which isn't what you would like

  2. As you are redirecting and testing for that redirect in the same machine anyway, you can simply write a note concerning the wrong slug in to the user's session and display it next time the vista template is made. Many frameworks possess a expensive messenger component, that permit you accomplish that easily, e.g. with Zend Framework you would employ the next code inside your controller action

    $flashMessenger = $this->_assistant->getHelper('FlashMessenger') $flashMessenger->addMessage('We did something within the last request')

  3. Rather than redirecting immediately whenever a wrong slug is located, you simply render a View template that informs your user concerning the wrong slug after which make use of a meta or javascript redirect in the template. Optionally, you'd also write an ordinary link to the best slug towards the template.

  4. And lastly, you might inject a custom header in to the redirect via header(), which could then be read out of your script. will not work either, because they'll be lost when the browser got the redirect in the server

An answer may be to determine the $_SERVER['HTTP_REFERER'] within the "new" page : if it's set and matches the "old" page, it's most likely since your user continues to be rerouted -- meaning you might like to display your message.

But observe that the Referer is distributed through the user's browser, and may be either disabled or forged -- therefore it is OK for doing things to boost the knowledge, however, you mustn't depend onto it for anything critical.

Found a functional solution:

  • Start session.
  • Check session, if $_SESSION['INVALID_SLUG'] is available display message then unset session

    else

  • Retreive slug from database e.g. /11/right-slug
  • Get current URI e.g. /11/wrong-slug
  • Compare, if different set $_SESSION['INVALID_SLUG'] and redirect to fix page

Any feedback? I understand this can not identify a 301 redirect itself more identify a trigger assosiated using the redirect.

Thanks for the help.

I believe the only real helpful option is by using a cookie.

Whenever a URL using the wrong slug is asked for, set a cookie for your URL with no slug:

$path = substr($_SERVER['REQUEST_URI'], 0, strrpos($_SERVER['REQUEST_URI'], '/')+1);
setcookie('wrong-slug', 1, 0, $path);

Then test if this type of cookie is available and display your message:

if (isset($_COOKIE['wrong-slug'])) {
    echo 'The location of this resource has changed. Please update your bookmarks.';
}