我正在使用ASP.NET 2.0 C#。我想将带有“www”的所有Web应用程序请求重定向到没有“www”
www.example.com to example.com
或者
example.com到www.example.com
Stackoverflow.com已经这样做了,我知道PHP(.htaccess)文件中有预制机制。但是如何在asp.net中做到这一点?
由于
答案 0 :(得分:27)
有关于此的Stackoverflow博客文章。
http://blog.stackoverflow.com/2008/06/dropping-the-www-prefix/
引用杰夫:
这是删除WWW的IIS7规则 来自所有传入URL的前缀。削减和 将此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>
答案 1 :(得分:6)
过去,当我无法修改IIS设置时,我已经使用了以下解决方案。
在HTTPModule(可能是最干净的)或Application_BeginRequest中的global.asax.cs或某些BasePage类型事件中,例如OnInit,我使用我希望使用的已知字符串对请求的URL执行检查:< / p>
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>
这种方法对我们很有效。
答案 2 :(得分:5)
接受的答案适用于单个网址或仅限几个网址,但我的应用程序提供数百个域名(手动输入的网址太多)。
这是我的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>
答案 3 :(得分:2)
为了回答这个问题,我们必须首先回顾一下WWW的定义:
万维网: ñ。缩写。 WWW
简洁地说,使用www子域是多余且耗时的通信。没有它,互联网,媒体和社会都会变得更好。
使用页面顶部的链接,您可以查看最近验证的域以及提交域以进行实时验证。
Apache Webserver:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Windows Server / IIS:
没有办法。
您可以使用Url Rewriter from Code Plex。使用相同的语法。
RewriteCond %{HTTP_HOST} !^(www).*$ [NC]
RewriteRule ^(.*)$ http://www.%1$1 [R=301]
答案 4 :(得分:0)
这通常由您的Web服务器直接在配置中处理。正如您所提到的,.htaccess文件为Apache Web服务器执行此操作 - 它与PHP无关。由于您使用的是ASP,因此几乎可以确定您的服务器是IIS。我知道有一种方法可以直接用IIS设置,但我不知道它是什么。您可以通过Google搜索与“IIS重定向”相关的内容而不是“ASP重定向”来帮助您进行搜索。
也就是说,你可以在PHP中完成它,几乎可以肯定是ASP,但是你必须在错误的域中点击任何URL来调用执行重定向操作的ASP脚本(使用适当的API调用或通过直接设置标题)。这将需要一些URL重写或服务器的某些部分,以便您的脚本处理错误主机上的所有URL ...首先直接在服务器上执行:)
答案 5 :(得分:0)
我们在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编写组件完成,但我们希望在此虚拟服务器上保留重定向。
答案 6 :(得分:0)
如果您使用的是IIS 7,只需导航到URL重写并添加规范域名规则。
P.S。它是为了确保您从domain.com重定向到www.domain.com
答案 7 :(得分:0)
此版本将:
<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>