繁体   English   中英

将web.config转换为.htaccess

[英]convert web.config to .htaccess

您好:我需要将IIS重写规则转换为.htaccess文件的帮助。 我对服务器规则不是很有经验(我主要设计和开发网站)。 我需要一些帮助。 我目前正在将网站(dolyn.com)从另一台服务器移到我们的网站,他们最初使用WIMP设置启动了该网站,因此使用IIS的URL重写(web.config),而不是Apache的mod_rewrite模块(.htacces)[我真的还不太了解它-我只是被要求确保它在我们的服务器上工作。 这是原始的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

除非需要身份验证,否则看起来好像不需要htpasswd文件(可能是这种情况,但是我在web.comfig中看不到任何内容。因此,在文档根目录的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

否则,该规则将导致错误。 如果您无权访问服务器/ vhost配置,请注释掉该规则,因为您将无法使用mod_rewrite强制使用小写字母。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM