301 redirect in StackOverflow. How does it works?

Let's take, for instance, the url of this question


If i change something in the final part of the URL, making it like


I get a 301 message and i'm redirected EXACTLY to the first URL i wrote.

This is reaaally SEO Friendly, IMHO. The question is: how did they do it? Is it possible to achieve it via .htaccess or there's another way to do it? In this case which way?

I'm not really expert, but it looks impossible to reach via htaccess...it looks more like

  1. I get the post ID from the URL
  2. I check the Database to see if the titles are equal
  3. If they're not, i rewrite the URL and send a 301 response


I am assuming they route the request through a page that checks the database for the seo-title.

RewriteRule ^questions/([0-9]+)/?$ routepage.php?id=$1 [R=301,L]
RewriteRule ^questions/([0-9]+)/(.*)/?$ routepage.php?id=$1&title=$2 [R=301,L]

And a file routepage.php

$page = getSeoTitleFromDatabase( $_GET['id'] );
if( $page != $_GET['page'] ) {
  header( "HTTP/1.1 301 Moved Permanently" );
  header( "Locations: /questions/" . $_GET['id'] . "/" . $page );
//Do whatever...

I am not aware of a way of checking the database with .htaccess or mod_rewrite.

Need Your Help

How to exit an infinite while loop when user taps screen?

java android while-loop

I currently have a while loop that starts when a user taps the screen. The loop is meant to be infinite, and should stop when it senses another screen tap from the user. (And, if tapped again, it'll

Hive ODBC Driver DSN setup Issue

odbc driver hive

I was trying to setup ODBC connection for Hive. I followed the below steps but it didn't worked.