1

How does a site like SO put the title of the question dynamically into the url bar.

Is this through rewritemapping and mod rewrite and how can I do it on a normal web host. Thanks

yehuda
  • 1,156
  • 1
  • 10
  • 21
  • For various flavors of slug generation routines, see previous SO questions: * http://stackoverflow.com/questions/2580581/best-way-to-escape-and-create-a-slug * http://stackoverflow.com/questions/2103797/url-friendly-username-in-php * http://stackoverflow.com/questions/2955251/php-function-to-make-slug-url-string * http://stackoverflow.com/questions/5409831/mysql-stored-function-to-create-a-slug And a few dozen more. – pp19dd Jun 05 '12 at 18:54

1 Answers1

3

In this URL:

https://stackoverflow.com/questions/10903041/how-to-automatically-put-title-in-url

the only part that matters is the question ID, 10903041. So just set up a redirection rule that ignores anything at the end of the URL, much like this:

RewriteRule /questions/(\d+) question.php?id=$1

The PHP script can then redirect to the correct title if the title is not already provided or is incorrect; much like what happens when you visit https://stackoverflow.com/questions/10903041/why-is-this-question-title-so-wrong?.

Community
  • 1
  • 1
Ry-
  • 199,309
  • 51
  • 404
  • 420
  • That is a good idea. But if i do a redirect from example.com/1928 to example.com/1928/fish-are-cool will that still gain me search engine juice? – yehuda Jun 05 '12 at 19:10
  • @yehuda: Yes, just make sure it's a 301. – Ry- Jun 05 '12 at 19:11
  • how can i do that? Also would i manually have to put hyphens in between the words? Thnx for your help btw – yehuda Jun 05 '12 at 19:12
  • can you just answer me how to send with header('location: ') function a 301? – yehuda Jun 05 '12 at 19:25
  • @yehuda: Yes, you would manually put hyphens between words, and I believe a `Location` header should do a 301 for you. – Ry- Jun 05 '12 at 22:01
  • Cheers. So I dont need to specify another header with a 301 or add it as a parameter to my header function? – yehuda Jun 06 '12 at 11:10