Apache 重写规则前导斜杠 [英] Apache rewrite rule leading slash
问题描述
前导斜杠第一个参数:被忽略?
两者之间的语法区别是什么
RewriteRule help help.php?q=noslash [L] #1RewriteRule/help help.php?q=withslash [L] #2
如果我点击 http://localhost/help,它会转到 #1,如果我点击 http://localhost//help 它仍然转到#1.
我是否说 RewriteRule 的第一个参数中的前导斜杠基本上被忽略了?
前导斜杠第二个参数:错误?
另外,为什么这个重写规则不起作用?
RewriteRule help/help.php [L] #1
在第二个参数前面放置一个前导斜杠实际上会为服务器创建一个 500 错误.为什么?
我应该注意到我正在使用 .htaccess 文件来编写这些规则
奇怪的是,
RewriteRule ^/help help.php?q=2 [L]
上述规则失败,永远不匹配.
这个规则:
RewriteRule ^help help.php?q=1 [L]
匹配 http://localhost/help、http://localhost//help 和 http://localhost///help一个>
RewriteRule 从未看到路径的前导斜线,正如 TheCoolah 所说 无论有多少,它们都会折叠起来(无论如何在使用 .htaccess 文件时都会折叠到 0 ..
对于问题的第二部分,
RewriteRule ^help/help.php
我从 Apache Mod_rewrite 权威指南中得到答案
<块引用>... 不以 http://或其他协议开头的重写目标假定指示符是文件系统路径.不以斜杠开头的文件路径被解释为相对于正在发生重写的目录.因此/help.php 在系统的根目录中查找名为 help.php 的文件,但在我的系统上找不到.
要使/help.php 显示为相对 URL(相对于站点的根目录),您可以使用 [PT] 指令:
RewriteRule ^/help/help.php [PT]
这会将 http://localhost/help 指向 http://localhost/help.php.
Leading slash first argument: ignored?
What's the syntax difference between
RewriteRule help help.php?q=noslash [L] #1
RewriteRule /help help.php?q=withslash [L] #2
If I hit http://localhost/help, it goes to #1, if I hit http://localhost//help it still goes to #1.
Am I right in saying the leading slash in the first argument to RewriteRule is essentially ignored?
Leading slash second argument: error?
Also, why doesn't this rewrite rule work?
RewriteRule help /help.php [L] #1
Putting a leading slash in front of the second arg actually creates a 500 error for the server. Why?
I should note I'm using a .htaccess file to write these rules in
Strangely enough,
RewriteRule ^/help help.php?q=2 [L]
The above rule fails and never matches.
This rule:
RewriteRule ^help help.php?q=1 [L]
Matches http://localhost/help, http://localhost//help and http://localhost///help
It appears RewriteRule never sees leading slashes of the path, and as TheCoolah said they are collapsed (to 0.. when using a .htaccess file anyway) no matter how many there are.
For the second part of the question,
RewriteRule ^help /help.php
I'm getting the answer from Definitive Guide to Apache Mod_rewrite
... a rewrite target that does not begin with http:// or another protocol designator is assumed to be a file system path. File paths that do not begin with a slash are interpreted as being relative to the directory in which the rewriting is taking place.
So /help.php looks in the root of the system for a file called help.php, which on my system it cannot find.
To make /help.php appear as a relative URL (relative to the root of the site) you can use the [PT] directive:
RewriteRule ^/help /help.php [PT]
That directs http://localhost/help to http://localhost/help.php.
这篇关于Apache 重写规则前导斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!