asp.net - 使用 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]
解决方案
以下是您提到的 .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 重写模块。如果没有,那么您可以从下面提到的链接下载它:
推荐阅读
- python-3.x - 当 EC2 实例自动扩展时,通过 PM2 运行 python API
- rest - 很奇怪的http删除休息操作——删除参数很多
- mysql - 使用 spring/springboot 将 JSON 数据从外部文件插入 mysql
- micronaut - 后端中断一段时间后,未恢复使用 micronaut 的跟踪
- hyperledger-fabric - 两种不同链码方法的不同交叉链码行为
- python - 将单个文件从子文件夹移到主文件夹
- c# - 将 C++ 数组和指针转换为 C#
- bootstrap-5 - Bootstrap 5 offcanvas 在关闭时滚动回顶部
- docker - Docker、Nginx 和代理通行证
- mongodb - 如何更新 mongodb 以使用 pymongo?