1

Background

As one would notice in stackoverflow's url structure, every question has an id followed by the question title. For example:

http://stackoverflow.com/questions/1039725/how-to-do-url-re-writing-in-php

The url above has the question id: 1039725 The question title is: how-to-do-url-re-writing-in-php

Just to play around with this url, even if one were to enter some junk stuff for the title part, the same question content will be displayed as long as the question id is there. For example the following url will also give the same content.

http://stackoverflow.com/questions/1039725/some-junk-stuff-title

and MORE IMORTANTLY the url bar automatically changes to:

http://stackoverflow.com/questions/1039725/how-to-do-url-re-writing-in-php

My Question

I understand that stackoverflow perhaps uses .htaccess to redirect all urls with the question id to some backend (let's say php page) to process it. But how does the address bar change to the actual question title automatically?

Most important thing is: how to achieve displaying the complete url without redirecting to any real folder? i.e. I don't want to use header() redirection in php to some folder.

The idea is to avoid creating unnecessary web folders for each question just for the sake of redirection.

Thejas
  • 25
  • 4
  • 1
    It's useful to note that while the default behavior of Apache when it receives a request for example.org/foo/bin.php is to look for a folder in the document root called 'foo' with a file inside called 'bin.php', this is only the default behavior, and is easy to change without needing to create individual files and folders for each page. This is the basis behind dynamic web pages. Check out [here](http://www.smashingmagazine.com/2011/11/02/introduction-to-url-rewriting/) or [here](https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/). You might want to google regex too – TheEnvironmentalist Feb 13 '15 at 10:03

2 Answers2

1

It's less magical than you might imagine. Every post has an associated slug in the database where it's stored (or that slug is generated on the fly, it doesn't matter). Only the numeric id is really important when retrieving from the database though. After that it's a simple check (in pseudocode):

$question = getFromDatabase($_GET['id']);
if ($question['slug'] != $_GET['slug']) {
    header("Location: /questions/$question[id]/$question[slug]", true, 301);
}

Nothing more, nothing less.

This is only done for SEO purposes, so the URL always contains the title of the question in its current form and to keep that URL canonical.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • Thanks deceze for your answer. Referring to your pseudocode, I actually don't care what the slug part looks like. So I just need to retrieve the question using the question id and display the content **BUT** without redirecting to a folder. I want to avoid creation of folders for each question. Idea is to serve the content based on just question id but change the url in the browser's address bar to `/questions//$question[id]/$question[slug]` – Thejas Feb 13 '15 at 09:31
  • URLs have nothing to do with "folders", you do not need to create folders to get a "pretty URL". See http://stackoverflow.com/q/20563772/476 – deceze Feb 13 '15 at 09:53
  • when you say in your pseudocode: `header("Location: /questions/$question[id]/$question[slug]", true, 301);` does this not redirect to a real folder? – Thejas Feb 13 '15 at 10:02
  • Nope, it redirects to **a URL**, and the web server can resolve this URL in any way you have configured it. The aforelinked question illustrates how to customize URL handling with .htaccess files on Apache. You cannot "change the URL just in the address bar". The URL in the address bar is the actual site you're currently on. – deceze Feb 13 '15 at 10:04
  • To match my "pseudocode" above, you'd configure Apache with something like: `RewriteRule /questions/(\d+)/(.*) questions.php?id=$1&slug=$2`. This is just an *extremely primitive example*, SO's web server is not running on PHP or Apache and probably doesn't do file lookups from URLs at all. A web server just *receives URLs, returns HTML*; files are in no way necessary at all. – deceze Feb 13 '15 at 10:09
  • Oh my! thanks @deceze! I never realized that the redirection can be called twice automatically - first time for resolving the correct pretty link and second time for serving the actual content. Thanks again! – Thejas Feb 13 '15 at 10:11
0

you can do this with the simple php code

check the code

$id=$_GET['id'];
$title=$_GET['title']

$dbposttitle=getfromdb($title)
if($dbposttitle==$title)
{
 /* code here */
}else
{
 header('location:yourdomain/$id/$dbposttitle');
}

this is the logic you can use for this

john
  • 567
  • 8
  • 23