문제

I have this link

www.example.com/1/title

which goes to

www.example.com/post.php?author=1&title=title

because of this rule

RewriteRule ^post/([0-9]+)/([\w-_:]+)/?$ post.php?author=$1&title=$2 [L,QSA,NC]

Fine, but now how do make it such that if somebody types in www.example.com/post.php?author=1&title=title to redirect to www.example.com/1/title

I have spent literally hours online researching this but the information is vague (at least for me) and not working.

2 things stump me so far:

  • Writing the pretty url into dynamic and then dynamic into pretty - Doesn't that create a loop?
  • I also wanted to go the route of a 301 redirect but I could not find any workable code that take variables from the first link to put into the redirect. In my head a 301 would be the right choice, but I see a lot of people (examples) doing it through RewriteRule.

I understand that (groups) can later be accessed by using $1 and $2... but when trying the reverse I cannot make it work. Eg:

RewriteRule ^post.php?author=([0-9]+)&title=([\w-_:]+)$ post/$1/$2

But like I said nothing works. I've been beating my head on sites like http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html but I cannot fully understand (or apply) what I'm reading there. Can you please let me know what I'm doing wrong or how I should approach this problem?

Many thanks for any help you can give me

도움이 되었습니까?

해결책

You need a new rule like this:

RewriteCond %{THE_REQUEST} \s/+post\.php\?author=([^&]*)&title=([^\s&]+) [NC]
RewriteRule ^ post/%1/%2? [R=302,L]

RewriteRule ^post/([0-9]+)/([\w-_:]+)/?$ post.php?author=$1&title=$2 [L,QSA,NC]
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top