如何编写像stackoverflow这样的.htaccess重定向是为了它的问题

时间:2011-01-03 08:43:45

标签: .htaccess

我正在尝试编写.htaccess规则,该规则会重定向某人要求

http://mysite.com/questions/123/my-question-name

http://mysite.com/questions/question_handler.php?qid=123

这是我到目前为止所写的内容(它不起作用):

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?(.*)$ [NC]
RewriteRule ^(.*)/questions/(\d+)/(.*)$ http://%1/questions/question_handler.php?qid=%2$1 [R=301,L]

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:4)

RewriteRule ^http://([^/]*)/questions/(\d+)/(.*)$ http://$1/questions/question_handler.php?qid=$2

您的(.*)可能过于贪心,所以它使用http://mysite.com/questions/123/my-question-name作为匹配的第一个群组

答案 1 :(得分:2)

我会做这样的事情

RewriteEngine on
RewriteRule ^questions/([0-9]+)/?$ questions/question_handler.php?qid=$1 [NC,L]