前导斜杠第一个参数:被忽略?
两者之间的语法差异是什么
RewriteRule help help.php?q=noslash [L] #1
RewriteRule /help help.php?q=withslash [L] #2
如果我点击http://localhost/help ,如果我点击http://localhost//help,它就会转到#1它仍然是#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 所说的 they are collapsed (无论如何,当使用 .htaccess 文件时为 0..)无论有多少个。
对于问题的第二部分,
RewriteRule ^help /help.php
我从 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.
因此/help.php 在系统根目录中查找名为 help.php 的文件,但在我的系统上找不到该文件。
要使/help.php 显示为相对 URL(相对于站点的根目录),您可以使用 [PT] 指令:
RewriteRule ^/help /help.php [PT]
关于Apache 重写规则前导斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4514627/