How to write .htaccess redirection as stackoverflow does for its questions

I am trying to write a .htaccess rule that redirects someone asking

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

to

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

Here is what I wrote so far (it does not work):

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 greatly appreciated.

+3
source share
2 answers

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

Yours (.*)was probably too greedy, so he used http://mysite.com/questions/123/my-question-name as the first group matched

+4

-

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

Source: https://habr.com/ru/post/1783184/


All Articles