如何mod_rewrite所以URL重定向其他方式?

时间:2016-06-29 03:12:22

标签: php apache .htaccess mod-rewrite

我有一个.htaccess文件,当用户在我的网站上注册时,他们会被重定向到example.com/profiles/[username]。丑陋的URL是example.com/profiles/?username=john,其中john可以是任何名称。每当用户搜索let {'1}}时,会显示404,所以我希望这个友好的URL重定向到一个丑陋的URL,以便显示他们的个人资料。所以我希望example.com/profiles/[username]在内部重定向到example.com/profiles/john

这是我的.htaccess条件:

example.com/profiles/?username=john

这些条件成功删除了网址的RewriteCond %{THE_REQUEST} /profiles/\?username=([^&\s]+) [NC] RewriteRule ^ /profiles/%1? [L,R=302] RewriteRule ^profiles/([\w-]+)/?$ /profiles/?username=$1 [L] 部分,我该怎么做呢?反过来呢?因此,当有人搜索?username=john时,它会在内部重定向到example.com/profiles/john,以便页面成功显示?

1 个答案:

答案 0 :(得分:0)

要将/profiles/?username=user重定向到/profiles.user,您可以使用

RewriteCond %{THE_REQUEST} /profiles/\?username=([^&\s]+) [NC]
RewriteRule ^ /profiles.%1? [L,R=302]

RewriteRule ^profiles\.([^/]+)/?$ /profiles/?username=$1 [L]