且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

阿帕奇重写规则斜线

更新时间:2023-09-18 23:45:04

奇怪的是,

 重写规则^ /帮助help.php?Q = 2 [L]

以上规则失败,永远不匹配。

这条规则:

 重写规则^帮助help.php?Q = 1 [L]

://本地主机/帮助的http://本地主机//帮助的http://本地主机///帮助

这似乎重写规则的从不的路径的斜线开头,并作为TheCoolah说看到they (反正使用.htaccess文件时0 ..)被折叠不管有多少。

有关的问题的第二部分,

 重写规则^帮助/help.php

我收到来自权威指南Apache的mod_rewrite


...重写目标,不以http://或其他协议
指示符被假定为一个文件系统路径。不以斜杠开头的文件路径PTED视为相对于在其中重写正在发生的跨目录$ P $。
块引用>

所以/help.php看在系统的根一个名为help.php文件,它在我的系统上无法找到。

要做出/help.php显示为相对URL(相对于网站的根),你可以使用[PT]指令:

 重写规则^ /帮助/help.php [PT]

这是指导的http://本地主机/帮助 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.