Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

how to write a .htaccess redirect like stackoverflow does for its questions

I'm trying to write a .htaccess rule that would redirect someone asking for

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

to

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

Here's what i wrote so far (it's not working):

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

Any help is much appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

Your (.*) was probably too greedy so it was using http://mysite.com/questions/123/my-question-name as the first group matched


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...