【问题标题】:Convert web.config file to .htaccess file将 web.config 文件转换为 .htaccess 文件
【发布时间】: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


【解决方案1】:

这是一个相当粗略的自动翻译。

  • Regex 规则应该很少进入 RewriteConds:

    RewriteCond %{PATH_INFO} ^/api/v1/
    RewriteRule (.*)    /controller.php
    

    每个都应该是公正的:

    RewriteRule ^api/v1/    /controller.php
    

    应省略前导 /(来自任何 ^/…)。

  • 在 Apache 上下文中环境引用需要 %

    RewriteRule (.*)    https://%{HTTP_HOST}%{REQUEST_URI}
                                ↑           ↑
    

    这个特别需要一个 RewriteCond 来只匹配 http:// 请求。

  • 很确定这些应该是正斜杠:

    RewriteRule .*    \app\$0
                      ↑   ↑
    
  • 保留原始 web.config 中的 cmets

【讨论】:

  • 谢谢!这让我离我更近了,但我仍然得到这个错误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.
猜你喜欢
  • 2017-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多