【发布时间】:2017-02-02 16:03:38
【问题描述】:
在通过不同子域/域提供的服务上,我希望能够只使用一个 .htaccess 文件。对于%{HTTP_HOST} 的每个可能值,我都设置了if 块,如下所示:
<if "%{HTTP_HOST} == 'staging.project.example.org'">
[detailed configuration...]
</if>
<if "%{HTTP_HOST} == 'development.project.example.org'">
[detailed configuration...]
</if>
这适用于以下访问配置行:
<if "%{HTTP_HOST} == 'staging.project.example.org'">
AuthType Basic
AuthUserFile ".htusers"
Require user example
Order deny,allow
Deny from all
Allow from xxx.xx.xx.xxx
</if>
但是 RewriteRule 这样的行会被 Apache 忽略:
<if "%{HTTP_HOST} == 'production.project.example.org'">
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/maintenance\.html$
RewriteRule ^(.*)$ https://www.project.example/maintenance.html [R=307,P]
</if>
是否可以在.htaccess 中使用RewriteRule 和if?我想可能是因为
"只有支持目录上下文的指令才能在内部使用 此配置部分。”
在https://httpd.apache.org/docs/2.4/en/mod/core.html#if
但是有没有办法解决这个问题?
【问题讨论】:
-
Mod_rewrite 确实支持目录上下文,因此它通过了该标准,尽管我并不是说它一定可以工作。当你说它不起作用时,以什么方式?尝试后会发生什么?
-
什么也没发生 -
if中的 RewriteRules 被忽略。 -
你应该只连接“If”语句或声明,如果检查这两个东西,也不要在 mod_rewrite 中混合 P 和 R 标志,因为它们用于不同的东西,你想重定向还是 rev 代理?
标签: apache .htaccess mod-rewrite