参数的mod重写规则 [英] mod rewrite rule for parameters

查看:96
本文介绍了参数的mod重写规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网址如下

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

我想将上述url永久重定向到使用.htaac​​cess文件. (Apache服务器)

i would like to permanently redirect above url to using .htaaccess file. (apache server)

www.localhost.com/first.last

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

please also consider there are few other urls there but i dont want to touch them..like

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

任何人都可以帮助我. 预先谢谢你.

can anyone please help me. thank you in advance.

推荐答案

您可以尝试使用RewriteCond处理查询字符串,并将捕获的匹配项传递给RewriteRule.您必须排除重写规则的任何.php脚本,否则它将对其他URL造成一些问题.

You could try to handle the Query String with RewriteCond and pass the captured match to RewriteRule. You must exclude any .phpscripts of your rewriting rule otherwise it will create some problems with others URLs.

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

Don't forget to add the [QSA] tag after your RewriteRule otherwise it will not add the Query String parameters.

也许正在做这样的事情:

Maybe doing something like this:

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.

这篇关于参数的mod重写规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆