我正在使用ASP.NET 2.0 C#。我想将所有带有"www"的Web应用程序请求重定向到没有"www"的请求。
www.example.com 重定向到 example.com
或者
example.com 重定向到 www.example.com
Stackoverflow.com已经实现了这一点,我知道在PHP(.htaccess)文件中有一个预制机制。但如何在ASP.NET中实现呢?
谢谢
我正在使用ASP.NET 2.0 C#。我想将所有带有"www"的Web应用程序请求重定向到没有"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/
引用Jeff的话:
以下是IIS7规则,用于从所有传入的URL中删除WWW前缀。将此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中(可能是最干净的方式),或者在global.asax.cs的Application_BeginRequest中,或者在一些BasePage类型的事件中,比如OnInit,我会对所请求的URL进行检查,使用已知的字符串来确定我想使用的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,但我的应用程序服务于数百个域名(手动输入的URL太多了)。
这是我的IIS7 URL Rewrite模块规则(此处的操作类型实际上是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>
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]
(涉及IT技术)这通常由您的Web服务器直接在配置中处理。正如您所提到的,.htaccess文件为Apache Web服务器执行此操作 - 它与PHP无关。由于您正在使用ASP,几乎可以确定您的服务器是IIS。我知道有一种方法可以直接使用IIS设置此项,但我不知道它是什么。您可能会通过知道您应该搜索与“ IIS重定向”相关的内容而不是“ ASP重定向”来帮助您进行搜索。
话虽如此,您可以在PHP中以及几乎肯定的ASP中执行此操作,但您必须使任何错误域名的URL都调用执行重定向操作的ASP脚本(使用适当的API调用或直接设置标头)。这将需要服务器进行一些URL重写或类似操作,以便所有错误主机上的URL都由您的脚本处理...最好直接在服务器上进行操作 :)
此版本将会:
<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>