我正在使用 ASP.NET 2.0 C#。我想将带有“www”的网络应用程序的所有请求重定向到不带“www”的
www.example.com 到 example.com
或者
example.com 到 www.example.com
Stackoverflow.com 已经在这样做了,我知道 PHP (.htaccess) 文件中有一个预制机制。但是如何在 asp.net 中做到这一点?
谢谢
我正在使用 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/
引用杰夫的话:
这是从所有传入 URL 中删除 WWW 前缀的 IIS7 规则。将此 XML 片段剪切并粘贴到您的 web.config 文件中
<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>
或者,如果您更喜欢使用 www 前缀,您也可以这样做:
<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>
过去,当我无法修改 IIS 设置时,我使用了以下解决方案。
在 HTTPModule(可能是最干净的)或 Application_BeginRequest 中的 global.asax.cs 或某些 BasePage 类型事件(例如 OnInit)中,我使用我希望使用的已知字符串对请求的 url 执行检查:
public class SeoUrls : IHttpModule
{
#region IHttpModule Members
public void Init(HttpApplication context)
{
context.PreRequestHandlerExecute += OnPreRequestHandlerExecute;
}
public void Dispose()
{
}
#endregion
private void OnPreRequestHandlerExecute(object sender, EventArgs e)
{
HttpContext ctx = ((HttpApplication) sender).Context;
IHttpHandler handler = ctx.Handler;
// Only worry about redirecting pages at this point
// static files might be coming from a different domain
if (handler is Page)
{
if (Ctx.Request.Url.Host != WebConfigurationManager.AppSettings["FullHost"])
{
UriBuilder uri = new UriBuilder(ctx.Request.Url);
uri.Host = WebConfigurationManager.AppSettings["FullHost"];
// Perform a permanent redirect - I've generally implemented this as an
// extension method so I can use Response.PermanentRedirect(uri)
// but expanded here for obviousness:
response.AddHeader("Location", uri);
response.StatusCode = 301;
response.StatusDescription = "Moved Permanently";
response.End();
}
}
}
}
然后在您的 web.config 中注册该类:
<httpModules>
[...]
<add type="[Namespace.]SeoUrls, [AssemblyName], [Version=x.x.x.x, Culture=neutral, PublicKeyToken=933d439bb833333a]" name="SeoUrls"/>
</httpModules>
这种方法对我们来说效果很好。
接受的答案适用于单个 URL 或仅几个,但我的应用程序提供数百个域名(手动输入的 URL 太多)。
这是我的 IIS7 URL 重写模块规则(这里的操作类型实际上是 301 重定向,而不是“重写”)。效果很好:
<rule name="Add WWW prefix" >
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www\.(.+)$" />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}/{R:1}"
appendQueryString="true" redirectType="Permanent" />
</rule>
为了回答这个问题,我们首先要回忆一下WWW的定义:
万维网:n。缩写。万维网
简而言之,使用 www 子域是多余的,而且沟通起来很耗时。没有它,互联网、媒体和社会都会变得更好。
使用页面顶部的链接,您可以查看最近验证的域以及提交域以进行实时验证。
阿帕奇网络服务器:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Windows Server/IIS:
没办法。
您可以使用来自 Code Plex 的 Url Rewriter。使用相同的语法。
RewriteCond %{HTTP_HOST} !^(www).*$ [NC]
RewriteRule ^(.*)$ http://www.%1$1 [R=301]
这通常由您的 Web 服务器直接在配置中处理。正如您所提到的,.htaccess 文件为 Apache Web 服务器执行此操作——它与 PHP 无关。由于您使用的是 ASP,因此几乎可以肯定您的服务器是 IIS。我知道有一种方法可以直接使用 IIS 进行设置,但我不知道它是什么。知道您应该在谷歌上搜索与“IIS 重定向”相关的内容,而不是“ASP 重定向”,这可能有助于您进行搜索。
也就是说,您可以在 PHP 中执行此操作,而且几乎可以肯定是 ASP,但是您必须在错误的域中访问任何 URL 调用执行重定向操作的 ASP 脚本(使用适当的 API 调用或通过直接设置标头)。这将需要在服务器方面进行一些 URL 重写或类似操作,以便错误主机上的所有 URL 都由您的脚本处理......首先直接在服务器上执行 :)
我们在 IIS 6 上非常简单地做到了这一点。我们基本上创建了第二个虚拟服务器,它上面除了自定义 404 页面 .aspx 页面之外什么都没有。该页面捕获了对 WHATEVERSERVER.com/whateverpage.aspx 的任何请求,并通过将 URL 更改为 www.whateverserver.com/whateverpage.aspx 重定向到真实服务器。
设置非常简单,并且它的优点是它可以与进入它的任何域一起使用(例如,如果您有多个域),而无需为每个域设置额外的规则。因此,对 www.myoldserver.com/xxx 的任何请求也将被重定向到 www.whateverserver.com/xxx
在 IIS 7 中,所有这些都可以通过 URL 编写组件来完成,但我们更愿意在这个虚拟服务器上保持重定向关闭。
如果您使用的是 IIS 7,只需导航到 URL 重写并添加规范域名规则。
PS 确保您从 domain.com 重定向到 www.domain.com
该版本将:
<rule name="Redirect to www" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="true">
<add input="{CACHE_URL}" pattern="^(.+)://" />
<add input="{HTTP_HOST}" negate="true" pattern="^www\.(.+)$" />
</conditions>
<action type="Redirect" url="{C:1}://www.{HTTP_HOST}/{R:1}" />
</rule>