0

我有以下网址

www.localhost.com/profile.php?username=first.last

我想将上面的 url 永久重定向到使用 .htaac​​cess 文件。(阿帕奇服务器)

www.localhost.com/first.last

还请考虑那里很少有其他网址,但我不想碰它们..like

www.localhost.com/message.php?id=12
www.localhost.com/editprofile.php?editname=first.last
www.localhost.com/uploadphoto.php?username=first.last

谁能帮帮我吗。先感谢您。

4

1 回答 1

1

您可以尝试处理查询字符串RewriteCond并将捕获的匹配传递给RewriteRule. 您必须排除.php重写规则的任何脚本,否则会在其他 URL 中产生一些问题。

不要忘记在你之后添加 [QSA] 标签,RewriteRule否则它不会添加查询字符串参数。

也许做这样的事情:

RewriteEngine on

#serve any existing php scripts as usual
RewriteRule ^([a-zA-Z0-9]+\.php) - [L]

#and now handle your specific stuff:
RewriteCond %{QUERY_STRING} ^([a-zA-Z0-9]+\./[a-zA-Z0-9]+)$
RewriteRule ^(%1)$ /profile.php?username=%1 [QSA]

I don't test it but it should be a good beginning. You can read some good stuff here and inside the docs for mod_rewrite httpd 2.2 about how to write and handle specific rewriting use cases.

于 2012-09-02T07:15:07.780 回答