apache - 为什么 RewriteRule x y.php 成功,但 RewriteRule x y.php/foo 失败?
问题描述
我正在使用现有的 .htaccess 文件对 Web 应用程序进行 dockerizing。在安装了 Apache 的 debian:stretch 中运行应用程序时,我遇到了 RewriteRules 的问题。
我把它归结为一个可以在这里说明的问题:
RewriteRule ^works$ /symfony/web/json.php [QSA,L]
RewriteRule ^fails$ /symfony/web/json.php/user [QSA,L]
第一个RewriteRule
将用户发送到symfony/web/json.php
Symfony 1 项目中的一个文件,在那里他们获得由 Symfony 处理的适当 404。
第二个RewriteRule
不会将用户发送到 Symfony 领域,通常会将尾随的“/user”交给控制器来处理。我只是得到一个“找不到文件。”。
相反,看起来更深目录中的 .htaccess 删除了路径的开头并将“/user”发送回根目录中的 .htaccess。
目录结构
root/
├── symfony/
│ ├── web/
│ │ ├── .htaccess
│ │ └── json.php
│ ├── .htaccess
└── .htacces
.htaccesses
root/.htaccess(相关位):
RewriteEngine On
RewriteBase /
RewriteRule ^works$ /symfony/web/json.php [QSA,L]
RewriteRule ^fails$ /symfony/web/json.php/user [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /symfony/web/index.php [QSA,L]
根/symfony/.htaccess(整个):
RewriteEngine On
RewriteRule ^(.*)$ /symfony/web/$1 [L]
root/symfony/web/.htaccess(相关位):
RewriteEngine On
RewriteBase /symfony/
RewriteCond %{REQUEST_URI} ^(/symfony/admin.php)
RewriteCond %{REMOTE_ADDR} !^10\.0
RewriteCond %{REMOTE_ADDR} !^10\.50
RewriteCond %{REMOTE_ADDR} !^10\.60
RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.1$
RewriteCond %{REMOTE_ADDR} !^::1$
RewriteRule .* / [L,R=403]
# we check if the .html version is here (caching)
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
# no, so we redirect to our front web controller
RewriteRule ^(.*)$ index.php [QSA,L]
重写日志
为了调试,我设置了LogLevel debug rewrite:trace3
. 以下是尝试转到 /fail 时日志中的相关位:
[rewrite:trace3] [rid#7f8c7807d0a0/initial] [perdir /home/wwwroot/root/] strip per-dir prefix: /home/wwwroot/root/fails -> fails
[rewrite:trace3] [rid#7f8c7807d0a0/initial] [perdir /home/wwwroot/root/] applying pattern '^works$' to uri 'fails'
[rewrite:trace3] [rid#7f8c7807d0a0/initial] [perdir /home/wwwroot/root/] strip per-dir prefix: /home/wwwroot/root/fails -> fails
[rewrite:trace3] [rid#7f8c7807d0a0/initial] [perdir /home/wwwroot/root/] applying pattern '^fails$' to uri 'fails'
[rewrite:trace2] [rid#7f8c7807d0a0/initial] [perdir /home/wwwroot/root/] rewrite 'fails' -> '/symfony/web/json.php/user'
[rewrite:trace2] [rid#7f8c7807d0a0/initial] [perdir /home/wwwroot/root/] trying to replace prefix /home/wwwroot/root/ with /
[rewrite:trace2] [rid#7f8c7807d0a0/initial] [perdir /home/wwwroot/root/] trying to replace context docroot /home/wwwroot/root with context prefix
[rewrite:trace1] [rid#7f8c7807d0a0/initial] [perdir /home/wwwroot/root/] internal redirect with /symfony/web/json.php/user [INTERNAL REDIRECT]
[rewrite:trace3] [rid#7f8c7805c4a8/initial/redir#1] [perdir /home/wwwroot/root/symfony/web/] add path info postfix: /home/wwwroot/root/symfony/web/json.php -> /home/wwwroot/root/symfony/web/json.php/user
[rewrite:trace3] [rid#7f8c7805c4a8/initial/redir#1] [perdir /home/wwwroot/root/symfony/web/] strip per-dir prefix: /home/wwwroot/root/symfony/web/json.php/user -> json.php/user
[rewrite:trace3] [rid#7f8c7805c4a8/initial/redir#1] [perdir /home/wwwroot/root/symfony/web/] applying pattern '.*' to uri 'json.php/user'
[rewrite:trace3] [rid#7f8c7805c4a8/initial/redir#1] [perdir /home/wwwroot/root/symfony/web/] add path info postfix: /home/wwwroot/root/symfony/web/json.php -> /home/wwwroot/root/symfony/web/json.php/user
[rewrite:trace3] [rid#7f8c7805c4a8/initial/redir#1] [perdir /home/wwwroot/root/symfony/web/] strip per-dir prefix: /home/wwwroot/root/symfony/web/json.php/user -> json.php/user
[rewrite:trace3] [rid#7f8c7805c4a8/initial/redir#1] [perdir /home/wwwroot/root/symfony/web/] applying pattern '^$' to uri 'json.php/user'
[rewrite:trace3] [rid#7f8c7805c4a8/initial/redir#1] [perdir /home/wwwroot/root/symfony/web/] add path info postfix: /home/wwwroot/root/symfony/web/json.php -> /home/wwwroot/root/symfony/web/json.php/user
[rewrite:trace3] [rid#7f8c7805c4a8/initial/redir#1] [perdir /home/wwwroot/root/symfony/web/] strip per-dir prefix: /home/wwwroot/root/symfony/web/json.php/user -> json.php/user
[rewrite:trace3] [rid#7f8c7805c4a8/initial/redir#1] [perdir /home/wwwroot/root/symfony/web/] applying pattern '^([^.]+)$' to uri 'json.php/user'
[rewrite:trace3] [rid#7f8c7805c4a8/initial/redir#1] [perdir /home/wwwroot/root/symfony/web/] add path info postfix: /home/wwwroot/root/symfony/web/json.php -> /home/wwwroot/root/symfony/web/json.php/user
[rewrite:trace3] [rid#7f8c7805c4a8/initial/redir#1] [perdir /home/wwwroot/root/symfony/web/] strip per-dir prefix: /home/wwwroot/root/symfony/web/json.php/user -> json.php/user
[rewrite:trace3] [rid#7f8c7805c4a8/initial/redir#1] [perdir /home/wwwroot/root/symfony/web/] applying pattern '^(.*)$' to uri 'json.php/user'
[rewrite:trace1] [rid#7f8c7805c4a8/initial/redir#1] [perdir /home/wwwroot/root/symfony/web/] pass through /home/wwwroot/root/symfony/web/json.php
[rewrite:trace3] [rid#7f8c780830a0/subreq] [perdir /home/wwwroot/root/] strip per-dir prefix: /home/wwwroot/root/user -> user
[rewrite:trace3] [rid#7f8c780830a0/subreq] [perdir /home/wwwroot/root/] applying pattern '^works$' to uri 'user'
[rewrite:trace3] [rid#7f8c780830a0/subreq] [perdir /home/wwwroot/root/] strip per-dir prefix: /home/wwwroot/root/user -> user
[rewrite:trace3] [rid#7f8c780830a0/subreq] [perdir /home/wwwroot/root/] applying pattern '^fails$' to uri 'user'
[rewrite:trace3] [rid#7f8c780830a0/subreq] [perdir /home/wwwroot/root/] strip per-dir prefix: /home/wwwroot/root/user -> user
[rewrite:trace3] [rid#7f8c780830a0/subreq] [perdir /home/wwwroot/root/] applying pattern '^(.*)$' to uri 'user'
[rewrite:trace2] [rid#7f8c780830a0/subreq] [perdir /home/wwwroot/root/] rewrite 'user' -> '/symfony/web/index.php'
[rewrite:trace2] [rid#7f8c780830a0/subreq] [perdir /home/wwwroot/root/] trying to replace prefix /home/wwwroot/root/ with /
[rewrite:trace2] [rid#7f8c780830a0/subreq] [perdir /home/wwwroot/root/] trying to replace context docroot /home/wwwroot/root with context prefix
[rewrite:trace1] [rid#7f8c780830a0/subreq] [perdir /home/wwwroot/root/] internal redirect with /symfony/web/index.php [INTERNAL REDIRECT]
所以它看起来像在根 .htaccess 中,RewriteRule ^fails$ /symfony/web/json.php/user
匹配并将请求重定向到 .htaccess 中symfony/web/
,找到匹配但删除 url 的开头,将“/user”发送回原始根 .htaccess。
解决方案
该问题是由 Apache 版本引起的。
我的 Apache 容器使用的是 debian:stretch Docker 映像,其中包括 Apache 2.4.25。我将基础映像更改为 debian:buster,其中包括 Apache 2.4.38。该更新解决了我的问题。
推荐阅读
- azure-functions - 证书的 Azure Function Key Vault 参考?
- angular - 如何获取查询列表
在 VIEW 中传递给服务方法 - sql - 试图为三个编号的序列返回一个 Count,
- javascript - 为什么导入 react-router-dom 会报错?找不到模块:无法解析“反应”
- javafx - 与自定义 CellFactory 节点交互将行添加到 TableView 选择列表?
- .net-core - 在没有身份的情况下使用 Azure AD 身份验证时无法更改“拒绝访问路径”
- c# - 有没有办法在我的编辑器中查看一个类的所有成员(或至少属性),包括它的基类?
- java - 使用 Java 更改计算器
- html - 3边有两个不同宽度的边框
- javascript - 如何从 Firebase 托管到 AWS Lambda 函数进行跨域请求