【发布时间】:2018-11-12 16:48:45
【问题描述】:
我有一个 web.config 文件,我尝试将其转换为 .htaccess,但它没有按预期工作。我希望得到一些帮助,找出我哪里出错了。
web.config
<rewrite>
<rules>
<rule name="Redirect to https">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>
<rule name="Blocker" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{PATH_INFO}" pattern="^/version_" />
</conditions>
<action type="Redirect" url="https://www.example.com/404/" redirectType="Permanent" appendQueryString="false" />
</rule>
<rule name="API Director">
<match url="(.*)" />
<conditions>
<add input="{PATH_INFO}" pattern="^/api/v1/" />
</conditions>
<action type="Rewrite" url="/controller.php" />
</rule>
<rule name="HoverCart App" stopProcessing="true">
<match url=".*" />
<conditions>
<!--<add input="{HTTP_HOST}" pattern="^(.*)(quiverstest.)" />-->
<add input="{PATH_INFO}" pattern="^/newrelic/" negate="true" />
</conditions>
<action type="Rewrite" url="\app\{R:0}" />
</rule>
</rules>
</rewrite>
.htaccess
RewriteRule (.*) https://{HTTP_HOST}{REQUEST_URI}
RewriteCond %{PATH_INFO} ^/version_
RewriteRule (.*) https://www.example.com/404/
RewriteCond %{PATH_INFO} ^/api/v1/
RewriteRule (.*) /controller.php
RewriteCond %{PATH_INFO} ^/newrelic/
RewriteRule .* \app\$0
特别是 /api/v1 不起作用。我在尝试拉入https://localhost/api/v1/app/时收到 404@
老实说,我对 web.config 或 .htaccess 都不太了解,我正在尝试在最初为 windows 机器制作的 mac 上设置一些东西。
编辑
我需要重写的最重要的规则就是这条
<rule name="API Director">
<match url="(.*)" />
<conditions>
<add input="{PATH_INFO}" pattern="^/api/v1/" />
</conditions>
<action type="Rewrite" url="/controller.php" />
</rule>
这是我得到的错误
The requested URL /api/v1/app/ was not found on this server.
Additionally, a 404 Not Found
error was encountered while trying to use an ErrorDocument to handle the request.
【问题讨论】:
-
RewriteRule (.*) https://{HTTP_HOST}{REQUEST_URI}我有点惊讶这不会导致终端循环 tbh - 除非 http 和 https 具有不同的 docroot(因此 .htaccess 规则)。 -
嗯,就像我说的那样,我对 web.config 和 .htaccess 的经验真的是 0,所以我不太确定:P 但是,它不会导致终端循环。而且我认为他们也没有不同的 docRoots
-
启用重写日志。所有
RewriteCond %PATH_INFO都是多余的。将实际的正则表达式移动到 RewriteRule(.*)的位置。 -
那会是什么样子? @马里奥
标签: php apache .htaccess iis web-config