301重定向的计算器。它是如何工作的? [英] 301 redirect in StackOverflow. How does it works?

查看:164
本文介绍了301重定向的计算器。它是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们举例来说,这个问题的网址

<$p$p><$c$c>http://stackoverflow.com/questions/20306229/301-redirect-in-stackoverflow-how-does-it-works

如果我改变的东西在URL中的最后一部分,使得它像

  http://stackoverflow.com/questions/20306229/301-r
 

我得到一个301的消息,我很确切地重定向到我写的第一个URL。

这是reaaally搜索引擎友好的,恕我直言。现在的问题是:他们是怎么做到的呢?是否有可能通过的.htaccess来实现它,或者还有另一种方式来做到这一点?在这种情况下,哪一种方式?

我并不是专家,但看起来不可能通过htaccess的到达......它看起来更像

  1. 我从网址
  2. 帖子ID
  3. 我检查数据库,看看标题是相等
  4. 如果他们没有,我重写URL和发送301响应
解决方案

我假设他们借道一个页面,检查数据库的搜索引擎优化称号的要求。

 重写规则^问题/([0-9] +)/ $ routepage.php?ID = $ 1 [R = 301,L]
重写规则^问题/([0-9] +)/ / $ routepage.php 1 ID = $&AMP(*);?标题= $ 2 [R = 301,L]
 

和文件 routepage.php

  $页= getSeoTitleFromDatabase($ _ GET ['身份证']);
如果($一页!= $ _GET ['页']){
  标题(HTTP / 1.1 301永久移动);
  标题(位置:/问题/。$ _ GET ['身份证']/$页);
}
//做什么?
 

我不知道与检查数据库的方式的.htaccess 的mod_rewrite

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.

这篇关于301重定向的计算器。它是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆