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

时间:2018-11-12 16:48:45

标签: php apache .htaccess iis web-config

我有一个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.

1 个答案:

答案 0 :(得分:3)

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

  • 正则表达式规则很少应包含在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的评论