简体   繁体   English

使用Apache Rewrite Module进行复杂的重写

[英]Using Apache Rewrite Module to do complex Rewriting

I want to use apache rewrite module to do complex rewriting. 我想使用apache重写模块来进行复杂的重写。

For example If the url is http://www.site.com/wall and http://www.site.com/profile and http://www.site.com/info I want to rewrite into http://www.site.com/gotopage.php?page=wall ,or http://www.site.com/gotopage.php?page=profile and so forth 例如,如果网址是http://www.site.com/wallhttp://www.site.com/profile以及http://www.site.com/info,我想重写为http:// www.site.com/gotopage.php?page=wall ,或http://www.site.com/gotopage.php?page=profile等等

But if the url is other than that I want to pass it the other way For example. 但是,如果网址不是我想以另一种方式传递它,例如。 if the url is http://www.site.com/newthing then it should rewrite as http://www.site.com/index.php?params=newthing 如果网址是http://www.site.com/newthing那么它应该重写为http://www.site.com/index.php?params=newthing

Please Help. 请帮忙。 I tried to see other questions but did not get it! 我试图看到其他问题,但没有得到它!

RewriteEngine On
# Special rule for 3 unique cases
RewriteRule /(wall|profile|info)$ /gotopage.php?page=$1 [L]

# Only rewrite if the URL has not yet been rewritten
RewriteCond %{HTTP_URL} (?!gotopage\.php|index\.php)
RewriteRule /([^/]+) /index.php?params=$1 [L]

Here's an explanation, line by line: 这是一个逐行的解释:

  1. Any URL that matches one of the 3 specified words gets rewritten to go to gotopage.php . 任何匹配3个指定单词之一的URL都会被重写为gotopage.php

  2. This line is a condition for the next line. 该行是下一行的条件。 Only if the URL matches this regex will the next line even be considered. 只有当URL与此正则表达式匹配时,才会考虑下一行。 The regex checks to ascertain that the request is not already going to gotopage.php or index.php . 正则表达式检查以确定请求尚未到达gotopage.phpindex.php If it is, we don't want to rewrite anything (we would just end up in an infinite loop). 如果是,我们不想重写任何东西(我们最终会以无限循环结束)。

  3. If the requested URL has no subsequent slashes, rewrite it to go to index.php . 如果请求的URL没有后续斜杠,请将其重写为index.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM