0

A sample URL of a question posted on StackOverflow is given below.

http://stackoverflow.com/questions/11227809/why-is-processing-a-sorted-array-faster-than-an-unsorted-array

The above URL is the complete URL to a question on StackOverflow, but you can also load the same page using the URL below (note that the description at the end is deleted):

http://stackoverflow.com/questions/11227809

Try loading the above URL and after the page loads, look at the address bar, it would look like the URL mentioned at first.

I think what happens when requesting with the 2nd URL is this:

  1. StackOverflow searches and finds a question with id 11227809.

  2. Then it redirects the user back to the original URL.

Are my assumptions correct? How can I do something like this with PHP? (just some background of how-to, and I know that stackoverflow is written in asp.net)

numaroth
  • 1,295
  • 4
  • 25
  • 36

1 Answers1

1

You can store slugs for contents with ids. If you look at $_SERVER you'll find the requested url. So if it does not contain the slug then you can look it up in the db and redirect to it.

fejese
  • 4,601
  • 4
  • 29
  • 36
  • In addition to that, I'd suggest you have some kind of rewrite rule to get the id (and ignore the slug). Try the following url: http://stackoverflow.com/questions/11227809/foo-bar as an example – Muc Aug 15 '13 at 17:57
  • It would be possible to maintain an URL map inside Apache, but its simpler to do this with a programming language. When you get a partial URL, redirect via a HTTP301. – Owen Beresford Aug 15 '13 at 22:12