将 web.config 文件转换为 .htaccess 文件

Convert web.config file to .htaccess file

我有一个 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 不工作。我在尝试拉入时收到 404 https://localhost/api/v1/app/

老实说,我对 web.config 或 .htaccess 都不太了解,我正在尝试在 mac 上设置一些最初为 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.

你的自动翻译相当粗糙。

  • 正则表达式规则应该很少进入 RewriteConds:

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

    每个都应该是:

    RewriteRule ^api/v1/    /controller.php
    

    The leading / should be omitted (from any ^/…).

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

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

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

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

    RewriteRule .*    \app$0
                      ↑   ↑
    
  • 保留原评论web.config