Apache 重写规则前导斜杠

标签 apache mod-rewrite

前导斜杠第一个参数:被忽略?

两者之间的语法差异是什么

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//helphttp://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]

指示http://localhost/helphttp://localhost/help.php .

关于Apache 重写规则前导斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4514627/

相关文章:

apache - 使用两个 htaccess 文件将 www 重写为非 www 不起作用

php - 安全转义 .htaccess mod_rewrite 正则表达式中的句点/点 (.) 字符

php - htaccess mod_rewrite 部分 url 到 GET 变量

html - 通过域不同的网站

linux - 如何准确跟踪服务器上的连接

apache - 如何以非 root 用户身份安装 Apache httpd?

apache - 服务器错误 500 PHP

apache - 使用 Apache mod_rewrite 检测空 REQUEST_URI 时出现问题

php - Linux Apache PHP : ERR_EMPTY_RESPONSE

apache - .htaccess 和 mod_rewrite 帮助