【问题标题】:Canonicalize URL with web.config使用 web.config 规范化 URL
【发布时间】:2020-10-11 10:33:24
【问题描述】:

我想规范化我的网址。我找到了这个 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 .htaccess iis web-config


    【解决方案1】:

    以下是您提到的 .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 规则粘贴到重写规则文本框中,您会在下面看到转换后的规则。

    More info关于这个功能。

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

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

    【讨论】:

      猜你喜欢
      • 2012-05-22
      • 1970-01-01
      • 2020-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-30
      • 2013-07-14
      相关资源
      最近更新 更多