问题
Let's take, for instance, the url of this question
http://stackoverflow.com/questions/20306229/301-redirect-in-stackoverflow-how-does-it-works
If i change something in the final part of the URL, making it like
http://stackoverflow.com/questions/20306229/301-r
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
- I get the post ID from the URL
- I check the Database to see if the titles are equal
- If they're not, i rewrite the URL and send a 301 response
回答1:
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
.
来源:https://stackoverflow.com/questions/20306229/301-redirect-in-stackoverflow-how-does-it-works