首页 > 解决方案 > 使用 web.config 规范化 URL

问题描述

我想规范化我的 URL。我找到了这个 Seo 网站检查器,说我的网站没有规范化。它显示在 .htaccess 上使用这些代码会有所帮助。但我的网站需要在 IIS 上的 web.config 上进行更改。谁能告诉我这些代码与 web.config 的等价物是什么?

将http://www.example.com重定向到http://example.com

RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ "http\:\/\/example\.com\/" [R=301,L]

并将http://example.com重定向到http://www.example.com

  RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
   RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

标签: asp.net.htaccessiisweb-config

解决方案


以下是您提到的 .htacess 规则的 web.config URL 重写规则:

 <rule name="Imported Rule 1" stopProcessing="true">
                <match url="^$" ignoreCase="false" />
                <conditions logicalGrouping="MatchAll">
                    <add input="{HTTP_HOST}" pattern="^www\.example\.com$" ignoreCase="false" />
                </conditions>
                <action type="Redirect" url="http://example.com" appendQueryString="true" redirectType="Permanent" />
            </rule>
            <rule name="Imported Rule 1-1" stopProcessing="true">
                <match url="^(.*)$" ignoreCase="false" />
                <conditions logicalGrouping="MatchAll">
                    <add input="{HTTP_HOST}" pattern="^www.example.com$" negate="true" />
                </conditions>
                <action type="Redirect" url="http://www.example.com/{R:1}" redirectType="Permanent" />
            </rule>

要将规则从 .htaccess 转换为 web.config,您可以使用 IIS URL 重写模块的导入功能:

1) 进入 IIS 管理器

2)在树中单击您的站点

3)双击功能视图中的URL重写

4)单击“操作”面板中的“导入规则”

5) 将您的 .htaccess 规则粘贴到重写规则文本框中,您会在下面看到转换后的规则。

有关此功能的更多信息。

确保您在 iis 中安装了 URL 重写模块。如果没有,那么您可以从下面提到的链接下载它:

https://www.iis.net/downloads/microsoft/url-rewrite


推荐阅读