.htaccess(mod_rewrite)和Microsoft azure

我在.htaccess中有这个代码,我需要将此转换为microsoft azure托管,因为它不起作用 .

RewriteEngine On

    RewriteCond %{THE_REQUEST} /index\.php[?/\s]
    RewriteRule ^index\.php$ /en/ [R=301,L]

    RewriteCond %{THE_REQUEST} /index_cz\.php[?/\s]
    RewriteRule ^index_cz\.php$ /cz/ [R=301,L]

    RewriteRule ^/en/?$ index.php [NC,L]
    RewriteRule ^cz/?$ index_cz.php [NC,L]

有人能帮帮我吗?多谢你们

回答(2)

3 years ago

您需要在适当的部分中使用重写规则创建web.config .

我从http://htaccesstowebconfig.com/生成了以下内容

<rule name="rule 1m" stopProcessing="true">
    <match url="^index\.php$"  />
    <action type="Rewrite" url="//en/"  />
</rule>
<rule name="rule 2m" stopProcessing="true">
    <match url="^index_cz\.php$"  />
    <action type="Rewrite" url="//cz/"  />
</rule>
<rule name="rule 3m" stopProcessing="true">
    <match url="^/en/?$"  ignoreCase="true" />
    <action type="Rewrite" url="/index.php"  />
</rule>
<rule name="rule 4m" stopProcessing="true">
    <match url="^cz/?$"  ignoreCase="true" />
    <action type="Rewrite" url="/index_cz.php"  />
</rule>

整个web.config文件看起来与此类似

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="rule 1m" stopProcessing="true">
                    <match url="^index\.php$"  />
                    <action type="Rewrite" url="//en/"  />
                </rule>
                <rule name="rule 2m" stopProcessing="true">
                    <match url="^index_cz\.php$"  />
                    <action type="Rewrite" url="//cz/"  />
                </rule>
                <rule name="rule 3m" stopProcessing="true">
                    <match url="^/en/?$"  ignoreCase="true" />
                    <action type="Rewrite" url="/index.php"  />
                </rule>
                <rule name="rule 4m" stopProcessing="true">
                    <match url="^cz/?$"  ignoreCase="true" />
                    <action type="Rewrite" url="/index_cz.php"  />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

3 years ago

我构建了一个简单的PHP项目来模拟重写规则的情况 .

根目录:

enter image description here

而像'en'这样的文件夹中 index.php 的内容是:

echo "response in en folder
"; var_dump($_GET);
我利用@ Joe Raio的答案进行了一些修改:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="rule 1m" stopProcessing="true">
                    <match url="^index\.php$"  />
                    <action type="Rewrite" url="/en/"  /> <!--modified remove '/'-->
                </rule>
                <rule name="rule 2m" stopProcessing="true">
                    <match url="^index_cz\.php$"  />
                    <action type="Rewrite" url="/cz/"  /> <!--modified remove '/'-->
                </rule>
                <rule name="rule 3m" stopProcessing="true">
                    <match url="^/en/?$"  ignoreCase="true" />
                    <action type="Rewrite" url="/index.php"  />
                </rule>
                <rule name="rule 4m" stopProcessing="true">
                    <match url="^/cz/?$"  ignoreCase="true" /> <!--modified add '/'-->
                    <action type="Rewrite" url="/index_cz.php"  />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

当我访问像 http://{web_site_name}.azurewebsites.net/index_cz.php?a=a&b=b 这样的网址时,它会在网站上返回:

response in cz folder array(2) { ["a"]=> string(1) "a" ["b"]=> string(1) "b" }

如有任何疑虑,请随时告诉我 .