如何mod_rewrite使URL重定向其他方式


How to mod_rewrite so URL redirects other way around?

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

这是我的。htaccess条件:

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

这些条件成功地删除了URL的?username=john部分,我如何使它成为另一种方式?所以当有人搜索example.com/profiles/john时,它会在内部重定向到example.com/profiles/?username=john,所以页面会成功显示?

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

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