如果请求自身,RewriteRule 不会按预期工作
RewriteRule does not work as expected if requesting itself
我要服务
http://172.16.0.2/container/app.html/any/path/here
by app.html
这是一个现有资源。
此外,我希望 index.html
提供我所有没有任何结束扩展的路径。即:http://172.16.0.2/container/path/randomly/written
这是我的规则:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_URI}:: ^(/.+)/(.*)::$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^[^.]{1}(?!<\.)[^.]{1,}$ index.html [L]
RewriteRule ^app.html$ - [L]
RewriteRule ^app.html(.*)$ app.html [L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule - [L]
- 请求
app.html
有效
- 请求
index.html
有效
- 请求
index.html/path/pathpath
有效(index.html)
- 请求
index.html/path/pathpath.EXT
有效 (404)
但是
- 请求
app.html/a/path
生成 404 错误。
完全没有头绪,谁能解释一下为什么?
日志
172.16.0.1 - - [27/Jul/2016:10:44:40 +0000] [172.16.0.2/sid#7fc44d87d4c0][rid#7fc44dbd10b8/initial] (3) [perdir /var/www/html/applications/container/web/] add path info postfix: /var/www/html/applications/container/web/app.html -> /var/www/html/applications/container/web/app.html/sdf/sdf
172.16.0.1 - - [27/Jul/2016:10:44:40 +0000] [172.16.0.2/sid#7fc44d87d4c0][rid#7fc44dbd10b8/initial] (3) [perdir /var/www/html/applications/container/web/] strip per-dir prefix: /var/www/html/applications/container/web/app.html/sdf/sdf -> app.html/sdf/sdf
172.16.0.1 - - [27/Jul/2016:10:44:40 +0000] [172.16.0.2/sid#7fc44d87d4c0][rid#7fc44dbd10b8/initial] (3) [perdir /var/www/html/applications/container/web/] applying pattern '^app.html(.*)$' to uri 'app.html/sdf/sdf'
172.16.0.1 - - [27/Jul/2016:10:44:40 +0000] [172.16.0.2/sid#7fc44d87d4c0][rid#7fc44dbd10b8/initial] (2) [perdir /var/www/html/applications/container/web/] rewrite 'app.html/sdf/sdf' -> 'app.html'
172.16.0.1 - - [27/Jul/2016:10:44:40 +0000] [172.16.0.2/sid#7fc44d87d4c0][rid#7fc44dbd10b8/initial] (3) [perdir /var/www/html/applications/container/web/] add per-dir prefix: app.html -> /var/www/html/applications/container/web/app.html
172.16.0.1 - - [27/Jul/2016:10:44:40 +0000] [172.16.0.2/sid#7fc44d87d4c0][rid#7fc44dbd10b8/initial] (1) [perdir /var/www/html/applications/container/web/] initial URL equal rewritten URL: /var/www/html/applications/container/web/app.html [IGNORING REWRITE]
问题是这些规则被设置到 <Directory>
指令中,在 <Directory>
指令之前的一些 RewriteRule
设置之后直接进入 <VirtualHost>
指令.
根据documentation,如果我在那里设置一个RewriteRule
,我只能通过指定URL来重写。不过,我是通过考虑物理路径来指定的。
Designates the location on the file-system of the resource to be
delivered to the client. Substitutions are only treated as a
file-system path when the rule is configured in server (virtualhost)
context and the first component of the path in the substitution is
exists in the file-system
两种解决方案:
- 考虑URL重写
- 将 RewriteRule 移到
<Directory>
指令之外,以便替换被 apache 很好地视为物理路径
我要服务
http://172.16.0.2/container/app.html/any/path/here
by app.html
这是一个现有资源。
此外,我希望 index.html
提供我所有没有任何结束扩展的路径。即:http://172.16.0.2/container/path/randomly/written
这是我的规则:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_URI}:: ^(/.+)/(.*)::$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^[^.]{1}(?!<\.)[^.]{1,}$ index.html [L]
RewriteRule ^app.html$ - [L]
RewriteRule ^app.html(.*)$ app.html [L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule - [L]
- 请求
app.html
有效 - 请求
index.html
有效 - 请求
index.html/path/pathpath
有效(index.html) - 请求
index.html/path/pathpath.EXT
有效 (404)
但是
- 请求
app.html/a/path
生成 404 错误。
完全没有头绪,谁能解释一下为什么?
日志
172.16.0.1 - - [27/Jul/2016:10:44:40 +0000] [172.16.0.2/sid#7fc44d87d4c0][rid#7fc44dbd10b8/initial] (3) [perdir /var/www/html/applications/container/web/] add path info postfix: /var/www/html/applications/container/web/app.html -> /var/www/html/applications/container/web/app.html/sdf/sdf
172.16.0.1 - - [27/Jul/2016:10:44:40 +0000] [172.16.0.2/sid#7fc44d87d4c0][rid#7fc44dbd10b8/initial] (3) [perdir /var/www/html/applications/container/web/] strip per-dir prefix: /var/www/html/applications/container/web/app.html/sdf/sdf -> app.html/sdf/sdf
172.16.0.1 - - [27/Jul/2016:10:44:40 +0000] [172.16.0.2/sid#7fc44d87d4c0][rid#7fc44dbd10b8/initial] (3) [perdir /var/www/html/applications/container/web/] applying pattern '^app.html(.*)$' to uri 'app.html/sdf/sdf'
172.16.0.1 - - [27/Jul/2016:10:44:40 +0000] [172.16.0.2/sid#7fc44d87d4c0][rid#7fc44dbd10b8/initial] (2) [perdir /var/www/html/applications/container/web/] rewrite 'app.html/sdf/sdf' -> 'app.html'
172.16.0.1 - - [27/Jul/2016:10:44:40 +0000] [172.16.0.2/sid#7fc44d87d4c0][rid#7fc44dbd10b8/initial] (3) [perdir /var/www/html/applications/container/web/] add per-dir prefix: app.html -> /var/www/html/applications/container/web/app.html
172.16.0.1 - - [27/Jul/2016:10:44:40 +0000] [172.16.0.2/sid#7fc44d87d4c0][rid#7fc44dbd10b8/initial] (1) [perdir /var/www/html/applications/container/web/] initial URL equal rewritten URL: /var/www/html/applications/container/web/app.html [IGNORING REWRITE]
问题是这些规则被设置到 <Directory>
指令中,在 <Directory>
指令之前的一些 RewriteRule
设置之后直接进入 <VirtualHost>
指令.
根据documentation,如果我在那里设置一个RewriteRule
,我只能通过指定URL来重写。不过,我是通过考虑物理路径来指定的。
Designates the location on the file-system of the resource to be delivered to the client. Substitutions are only treated as a file-system path when the rule is configured in server (virtualhost) context and the first component of the path in the substitution is exists in the file-system
两种解决方案:
- 考虑URL重写
- 将 RewriteRule 移到
<Directory>
指令之外,以便替换被 apache 很好地视为物理路径