2012-06-05 17 views
1

像SO这样的网站如何动态地将问题的标题放入网址栏中。如何自动将标题放在网址中

这是通过rewritemapping和国防部重写,我怎么能在一个正常的网络主机上做到这一点。由于

+0

对于各种口味的slu generation生成例程,请参阅上一个问题:* http://stackoverflow.com/questions/2580581/best-way-to-escape-and-create-a-slug * http:// stackoverflow.com/questions/2103797/url-friendly-username-in-php * http://stackoverflow.com/questions/2955251/php-function-to-make-slug-url-string * http:// stackoverflow.com/questions/5409831/mysql-stored-function-to-create-a-slug还有几十个。 – pp19dd

回答

3

在这个网址:

https://stackoverflow.com/questions/10903041/how-to-automatically-put-title-in-url 

重要的是问题的ID,10903041的唯一部分。所以刚刚成立,在该URL的末尾忽略任何一个重定向规则,就像这样:

RewriteRule /questions/(\d+) question.php?id=$1 

然后PHP脚本可以重定向到正确的标题,如果标题是不是已经提供或不正确;很像当您访问https://stackoverflow.com/questions/10903041/why-is-this-question-title-so-wrong?时发生的情况。

+0

这是一个好主意。但是,如果我做了从example.com/1928到example.com/1928/fish-are-cool的重定向,那仍然会让我获得搜索引擎果汁吗? – yehuda

+0

@yehuda:是的,只要确保它是301. – Ryan

+0

我该怎么做?还我手动不得不在连接词之间加上连字符? thnx你的帮助btw – yehuda