【问题标题】:convert web.config to .htaccess将 web.config 转换为 .htaccess
【发布时间】:2013-12-17 21:10:21
【问题描述】:

您好:我需要帮助将我的 IIS 重写规则转换为 .htaccess 文件。 我对服务器规则不是很有经验(我主要设计和开发网站)。 我需要一些帮助。我目前正在将一个网站 (dolyn.com) 从另一台服务器转移到我们的网站,他们最初使用 WIMP 设置启动它,因此使用来自 IIS (web.config) 的 URL 重写而不是来自 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

【问题讨论】:

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


    【解决方案1】:

    除非您需要身份验证,否则您似乎不需要 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
    

    否则,该规则将导致错误。如果您无权访问您的服务器/虚拟主机配置,那么只需注释掉该规则,因为您将无法使用 mod_rewrite 强制小写。

    【讨论】:

    • 它看起来像是重定向到 www.dylon.com - 我想要的是它在 web01.dnsnetworks.ca/dolyn 下工作(这是所有文件所在的地方...... index.php) ..
    • 当我尝试加载站点时……这是错误日志中弹出的内容; PHP 致命错误:require_once():在 /app/ 中打开所需的 '/app/website/doly/_includes/common.php' (include_path='.:/usr/share/pear:/usr/share/php') 失败website/dolyn/index.php 在第 13 行我想我错过了服务器上的 php-devel 库,因为它试图使用 PHP 的 php-pear 扩展。它不是一个简单的安装,因为我正在运行最新版本的 PHP,而且我的系统的开发库还不可用……。你怎么看@JonLin
    • @user3112402 有很多不同的 PHP 库没有附带最小的 PHP 安装,很多。但是您可能应该确保/app/website/doly/_includes/common.php 存在
    • @user3112402 运行 apache/php 的任何用户是否对该文件具有读取权限?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-05
    • 1970-01-01
    • 1970-01-01
    • 2012-01-30
    相关资源
    最近更新 更多