help regarding dynamic redirect rule in htaccess

喜夏-厌秋 提交于 2019-12-11 05:49:20

问题


I need ur help for given subject. I am playing with htaccess rules first time in life.

here is the scene -

i want to redirect the urls -

 http://www.abc.com/var1
 http://www.abc.com/var2

to follwing urls -

 http://www.abc.com/index.php?u=var1
 http://www.abc.com/index.php?u=var2

In this case the values var1 & var2 can be anything (i.e. string which will contain only alphanumeric characters.)

One more thing -

I want to skip this rule if the url is -

http://www.abc.com/SKIPME

Please help me to write this rule!

Regards, Shahu!


回答1:


You would be better off defining a URI schema that tells you when something WILL be rewritten. For example...

 http://www.abc.com/site/var1
 http://www.abc.com/site/var2

That way, you can ensure you only ever apply the rule if the psuedo "site/" directory is browsed and not affect any other URI. This is the rewrite rule based on the above schema.

RewriteEngine on
RewriteRule ^site/([^/\.]+)/?$ index.php?u=$1 [L,NC,QSA]

Any other address, other than "/site/.../" would be unaffected by this rule, which means you don't need to worry about setting some addresses to be avoided. This keeps things as simple as possible.

You don't have to use "site" - you can use whatever name fits your purpose.



来源:https://stackoverflow.com/questions/2688689/help-regarding-dynamic-redirect-rule-in-htaccess

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