Microsoft rewriting module - Force www on url Or remove www from url(Microsoft 重写模块 - 在 url 上强制 www 或从 url 中删除 www)
问题描述
我有一个与 Windows Server 2008 和 IIS7.5 共享的主机计划,并且安装并启用了 Microsoft 重写模块.
I have a shared hosting plan with Windows Server 2008 and IIS7.5, and there is Microsoft rewriting module installed and enabled.
<rewrite>
<rules>
<rule name="myRule" patternSyntax="Wildcard">
<!--Rewriting code-->
</rule>
</rules>
</rewrite>
那么,如何使用 Microsoft 重写模块将 mydomain.com/everywhere-in-site/my-page.html 重定向到 www.mydomain.com/everywhere-in-site/my-page.html?
So, how to redirect mydomain.com/everywhere-in-site/my-page.html to www.mydomain.com/everywhere-in-site/my-page.html with Microsoft rewriting module?
如果我想将 www.mydomain.com/everywhere-in-site/my-page.html 重定向到 mydomain.com/everywhere-in-site/my-page.html 怎么办?
And what if I want to redirect www.mydomain.com/everywhere-in-site/my-page.html to mydomain.com/everywhere-in-site/my-page.html ?
推荐答案
要从域中删除 www 并重定向到裸域",您可以像以下代码片段一样进行操作:
To remove the www from a domain and redirect to a "naked domain" you could di it like in the following code snippet:
<rewrite>
<rules>
<rule name="Remove WWW prefix" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.yourdomain.com$" />
</conditions>
<action type="Redirect" url="http://yourdomain.com/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
反过来(如果您愿意的话)将非 www 重定向到带有 www 的:
And the other way around (if you prefer that) to redirect a non-www to one with www:
<rewrite>
<rules>
<rule name="Add WWW prefix" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="^yourdomain.com$" />
</conditions>
<action type="Redirect" url="http://www.yourdomain.com/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
redirectType="Permanent" 当然是可选的,但对于 SEO 和大多数情况我会推荐它.
The redirectType="Permanent" is of course optional but for SEO and most scenarios I would recommend it.
另请参阅这些 SO 问题/答案:
Please see also these SO questions/answers:
- IIS7 URL 重写 - 添加www"前缀
- 转发 http://mydomain.com/ctrlr/act/val 到 http://WWW.mydomain.com/ctrlr/act/val
- 从地址中删除 www 的正确方法使用 IIS URL 重写
这篇关于Microsoft 重写模块 - 在 url 上强制 www 或从 url 中删除 www的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Microsoft 重写模块 - 在 url 上强制 www 或从 url 中删除 www
基础教程推荐
- 将数据集转换为列表 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- 如果条件可以为空 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
