mod_rewrite 条件的基本帮助
你好。我是 mod_rewrite 的新手,想知道是否可以执行以下操作:
RewriteRule ^([^/]*)$ index.php?slug=$1
此规则仅指向 index.php 但如果我想做另一条规则,将特定的 slug 指向不同的脚本,即
RewriteRule ^a-new-page$ different.php
这将不起作用,因为第一条规则声明输入的任何内容都应指向索引。
有没有办法对特定的蛞蝓强制执行新规则?
Hi. I am new to mod_rewrite and was wondering if it was possible to do the following:
RewriteRule ^([^/]*)$ index.php?slug=$1
This rule will point only at index.php but if I wanted to do another rule that pointed a specific slug to a different script, i.e.
RewriteRule ^a-new-page$ different.php
This would not work because the first rule has declared anything that is entered should point at index.
Is there a way to force the new rule for that specific slug?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这是可能的——只需将此类特定规则放在通用/广义规则之前(规则声明的顺序很重要):
另请注意广义规则——它们可能会进入一个重写循环(重写新 URL 后进入下一次迭代,如果规则编写不正确,可能会进入无限循环,Apache 将不得不强制终止,您的用户将看到 500 错误页面)。更好的规则是:
[L]
告诉 Apache 停止重写[QSA]
告诉将查询字符串附加到新 URL(如果您有 URL 参数,则很有用) :例如跟踪数据、页面参数等)。有用的链接:http://httpd.apache.org/docs/current/rewrite/
Yes, it is possible -- just place such specific rule before generic/broad one (the order in which rules are declared matters):
Also please pay attention to the broad rules -- they may enter into a rewrite loop (after rewrite new URL goes into next iteration, and if rule is not written right it may enter endless loop which Apache will have to forcedly terminate and your user will see an 500 error page instead). Better rule will be:
[L]
tells Apache to stop rewriting if this rule matches[QSA]
tells to append query string to a new URL (useful if you have URL parameters: e.g. tracking data, page parameters etc).Useful Link: http://httpd.apache.org/docs/current/rewrite/