c# - 如何使用 "www"URL 重定向到没有 "www"URL,反之亦然?

标签 c# asp.net redirect canonical-name

我正在使用 ASP.NET 2.0 C#。我想将所有带有“www”的网络应用程序请求重定向到不带“www”

www.example.com 到 example.com

或者

example.com 到 www.example.com

Stackoverflow.com 已经在这样做了,我知道 PHP (.htaccess) 文件中有一个预制机制。但是如何在 asp.net 中做到这一点?

谢谢

最佳答案

有一篇关于此的 Stackoverflow 博客文章。

https://blog.stackoverflow.com/2008/06/dropping-the-www-prefix/

引用杰夫的话:

Here’s the IIS7 rule to remove the WWW prefix from all incoming URLs. Cut and paste this XML fragment into your web.config file under

<system.webServer> / <rewrite> / <rules>

<rule name="Remove WWW prefix" >
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.domain\.com" />
</conditions>
<action type="Redirect" url="http://domain.com/{R:1}"
    redirectType="Permanent" />
</rule>

Or, if you prefer to use the www prefix, you can do that too:

<rule name="Add WWW prefix" >
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="^domain\.com" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:1}"
    redirectType="Permanent" />
</rule>

关于c# - 如何使用 "www"URL 重定向到没有 "www"URL,反之亦然?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/521804/

相关文章:

c# - 取消任务会引发异常

c# - 如何将 ServiceStack RegistrationFeature 插件与 Redis 一起使用?

工作站锁定时,C# 窗体不会在 Microsoft Windows 上最小化

asp.net - googlebot在抓取时会保留 session 吗?

regex - nginx 重写排除

c# - 防止在 ASP.NET MVC 页面上重复提交/发布

javascript - 带有 JSON.Stringify 的 ASP.NET Core 2.2 AntiForgeryToken

asp.net - 有谁知道 Windows Azure WebResource.axd 时区错误的解决方法?

.htaccess - 基于 IP 地址限制/阻止目录

.htaccess - 将子域重定向到顶级域的文件夹