I am using ASP.NET 2.0 C#. I want to redirect all request for my web app with \"www\" to without \"www\"
www.example.com to example.com
Or
example.co
I've gone with the following solution in the past when I've not been able to modify IIS settings.
Either in an HTTPModule (probably cleanest), or global.asax.cs in Application_BeginRequest or in some BasePage type event, such as OnInit I perform a check against the requested url, with a known string I wish to be using:
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();
}
}
}
}
Then register the class in your web.config:
<httpModules>
[...]
<add type="[Namespace.]SeoUrls, [AssemblyName], [Version=x.x.x.x, Culture=neutral, PublicKeyToken=933d439bb833333a]" name="SeoUrls"/>
</httpModules>
This method works quite well for us.
This is usually handled by your web server directly in the configuration. As you mentioned, the .htaccess file does this for the Apache web server -- it has nothing to do with PHP. Since you're using ASP, it's a near certainty your server is IIS. I know there is a way to set up this direct with IIS, but I don't know what it is. You may be aided in your search by knowing you should be googling for things related to "IIS redirect", not "ASP redirect".
That said, you CAN do it in PHP, and almost certainly ASP as well, but you'll have to have hitting any URL at the wrong domain invoke an ASP script that performs the redirect operation (using appropriate API calls or by setting headers directly). This will necessitate some URL rewriting or somesuch on the part of the server so that all URLs on the wrong host are handled by your script... just do it directly at the server in the first place :)
This version will:
<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>
In case you are using IIS 7, simply navigate to URL rewrite and add the canonical domain name rule.
P.S. Its to make sure that you get redirected from domain.com to www.domain.com
In order to answer this question, we must first recall the definition of WWW:
World Wide Web: n. Abbr. WWW
Succinctly, use of the www subdomain is redundant and time consuming to communicate. The internet, media, and society are all better off without it.
Using the links at the top of the page, you may view recently validated domains as well as submit domains for real-time validation.
Apache Webserver:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Windows Server/IIS:
There is no way.
You can use Url Rewriter from Code Plex. With the same syntax.
RewriteCond %{HTTP_HOST} !^(www).*$ [NC]
RewriteRule ^(.*)$ http://www.%1$1 [R=301]
Source
The accepted answer works for a single URL or just a few, but my application serves hundreds of domain names (there are far too many URLs to manually enter).
Here is my IIS7 URL Rewrite Module rule (the action type here is actually a 301 redirect, not a "rewrite"). Works great:
<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>