将web.config转换为.htaccess

时间:2013-12-17 21:10:21

标签: apache .htaccess iis mod-rewrite web-config

您好:我需要帮助将我的IIS重写规则转换为.htaccess文件。 我对服务器规则不太熟悉(我主要设计和开发网站)。 我需要一些帮助。我目前正在通过一个网站(dolyn.com)从另一个服务器移动到我们的网站,他们最初使用WIMP设置启动它,因此使用IIS重写URL(web.config)而不是Apache(.htacces)的mod_rewrite模块[我真的不太了解它的后勤 - 我刚被要求确保它在我们的服务器上运行。这是原始的web.config文件:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>

    <!-- Doesn't work on DEV servers, uncomment it on live site -->
    <!-- httpErrors errorMode="Detailed" /-->

        <rewrite>
            <rules>
                <clear />

                <!--
                This rule will redirect all requests to a single main domain as specified in the 1st condition, except if it's on a baytek dev site.
                Instructions:
                    1. Enable the rule
                    2. Change "^www.maindomain.com$" to real domain, ex. "^www.bayteksystems.com$", in both places.
                -->

                <rule name="Redirect to WWW" stopProcessing="true" enabled="false">
                    <match url=".*" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{HTTP_HOST}" pattern="^www.maindomain.com$" negate="true" />
                        <add input="{HTTP_HOST}" pattern="dev.bayteksystems.com$" negate="true" />
                    </conditions>
                    <action type="Redirect" url="http://www.maindomain.com/{ToLower:{R:0}}" redirectType="Permanent" />
                </rule>

                <rule name="Add trailing slash" stopProcessing="true">
                    <match url="(.*[^/])$" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Redirect" url="{ToLower:{R:1}}/" redirectType="Permanent" />
                </rule>

                <rule name="Convert to lower case" stopProcessing="true">
                    <match url=".*[A-Z].*" ignoreCase="false" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Redirect" url="{ToLower:{R:0}}" redirectType="Permanent" />
                </rule>

                <rule name="DynamicURLs" enabled="true" stopProcessing="true">
                    <match url="^(.*)$" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{URL}" pattern="^_cms.*" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.php?page={R:1}&amp;url_rewrite=true" />
                </rule>

            </rules>
        </rewrite>

    </system.webServer>
</configuration>

我一直在做相当多的研究,看起来我可能还需要一个.htpasswd文件?它是否正确?你可以从web.config中找到它来自的服务器baytechsystems.com,我们将它移到dnsnetworks.ca

1 个答案:

答案 0 :(得分:0)

看起来你不需要htpasswd文件,除非你需要身份验证(可能是这种情况,但我在web.com上没有看到任何内容。所以在你的文档根目录中的htaccess文件中有这样的东西:

RewriteEngine On

# Redirect to WWW
RewriteCond %{HTTP_HOST} !www\.dnsnetworks\.ca$ [NC]
RewriteCond %{HTTP_HOST} !dev\.dnsnetworks\.ca$ [NC]
RewriteRule ^(.*)$ http://www.dnsnetworks.ca/$1 [L,R=301]

# Add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+[^/])$ /$1/ [L,R=301]]

# Convert to lower case
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*[A-Z].*)$ /${tolower:$1} [L,R=301] 

# DynamicURLs
RewriteCond %{REQUEST_URI} !^/_cms
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?page=$1&url_rewrite=true [L]

这里唯一的问题是“转换为小写”部分。需要在服务器配置中定义tolower函数。像这样:

RewriteMap tolower int:tolower

否则,该规则将导致错误。如果您无权访问server / vhost配置,那么只需注释掉该规则,因为您无法使用mod_rewrite强制执行小写。