我正在使用 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/