drau89457 2014-07-09 14:46
浏览 28

澄清这个RewriteRule Apache HTTP mod_rewrite?

I would like to ask some basic clarifications on this mod_rewrite RewriteRule of Apache, cause I have found different explanations and maybe I am missing something:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !\.(js|ico|gif|jpg|png|css)$ /index.php

This rule is supposed to rewrite every request to the index.php (in the DOCUMENT_ROOT directory) when the requested resource extension is NOT one of those hardcoded in the sub-pattern of the regexp at the condition that the resource is NOT a local file, but what differs this rule from this one:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php

? They're exactly the same if not that the substitution string in the second rule (index.php) is without the leading slash.

Now, a user here -> Apache rewrite rule leading slash says that:

So /help.php looks in the root of the system for a file called help.php, which on my system it cannot find.

Shouldn't it be so even for the /index.php (the substitution string in the first rule I have posted)?

Then I would like to ask: if the request is for example to http://localhost/a_file_that_don't_exist.php Apache is going to match against a_file_that_don't_exist.php or against /a_file_that_don't_exist.php?

In the wiki -> https://wiki.apache.org/httpd/RewriteRule they say that Apache will:

match against the URI-path

That is:

everything after http://hostname, and not including any query string arguments

But in some tutorials about RewriteRule people say that URI-path is everything after http://hostname/, thus NOT including the leading slash (REQUEST_URI without the leading slash).

Could someone elucidate this misunderstandings, please?

Cause if pattern matches without the leading slash, the first RewriteRule should replace the URI-path to -> http://localhost//index.php which is not a valid URL, but instead it works beautifully both ways...

EDIT: in the Apache docs they say:

Note that mod_rewrite tries to guess whether you have specified a file-system path or a URL-path by checking to see if the first segment of the path exists at the root of the file-system. For example, if you specify a Substitution string of /www/file.html, then this will be treated as a URL-path unless a directory named www exists at the root or your file-system (or, in the case of using rewrites in a .htaccess file, relative to your document root), in which case it will be treated as a file-system path.

So is it correctly to say that in .htaccess RewriteRules are always relative to the DOCUMENT_ROOT if they start with a leading slash and relative to the .htaccess directory if without the leading slash?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 Revit2020下载问题
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大