mod rewrite rule for parameters

ⅰ亾dé卋堺 提交于 2019-12-13 05:42:31

问题


I have below url(s)

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

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

www.localhost.com/first.last

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.


回答1:


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.

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.



来源:https://stackoverflow.com/questions/12233947/mod-rewrite-rule-for-parameters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!