문제

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

  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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top