重写规则始终重定向到错误的URL

时间:2013-10-24 06:47:47

标签: apache .htaccess mod-rewrite url-rewriting url-rewrite-module

大家好,
我需要将http://mysite.com/user/profile/following?profile_name=MYNAME重写为http://mysite.com/user/profile/MYNAME/following

我写了这样的规则:

RewriteRule user/profile/(.*)/(.*) /user/profile/$2?profile_name=$1 [L,R=301]  

当我在浏览器中添加http://mysite.com/user/profile/MYNAME/following之类的网址时,它始终会将我重定向到http://mysite.com/user/profile/following?profile_name=MYNAME

我错过了什么?

提前致谢

1 个答案:

答案 0 :(得分:0)

您可以使用此代码:

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+user/profile/([^?]+)\?profile_name=([^\s&]+) [NC]
RewriteRule ^ /user/profile/%1/%2? [R=301,L]

# internal forward from pretty URL to actual one
RewriteRule ^user/profile/([^/]+)/([^/]*)/?$ /user/profile/$2?profile_name=$1 [L,NC,QSA]