Windows Server Redirect Issue
I am dealing with some client's that use Windows servers and as such do not support .htaccess files. This is not a huge deal but, my concern is this:
I have a rule set up in my .htaccess file to redirect the non-www version of the site to the www version. This makes the URL's look nicer and prevents duplicate content being indexed.
However, there does not seem to be a simple way to开发者_开发问答 do this on a Windows server. I have read through tutorials on setting up a web.config file but, my Windows server experience is very limited and many times I only have FTP access to the site (no server access).
Any ideas on a quick and fairly simple solution, that I could use?
Create web.config (in the root directory) file with the next content:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<rewrite>
<rules>
<rule name="CanonicalHostNameRule" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.domain\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
The URL Rewrite Module (at least version 2.0) has to be installed.
To use custom module create file CanonicalRedirectModule.cs
in folder App_Code
it the root folder of the Web Site Application with next content:
using System;
using System.Web;
public class CanonicalRedirectModule : IHttpModule
{
public const string configKeyCanonicalHostName = "CanonicalHostName";
private string configCanonicalHostName;
#region IHttpModule Members
public void Dispose()
{
}
public void Init(HttpApplication context)
{
this.configCanonicalHostName = System.Configuration.ConfigurationManager.AppSettings[configKeyCanonicalHostName];
if (string.IsNullOrEmpty(this.configCanonicalHostName))
{
System.Diagnostics.Trace.TraceWarning("Can't find application setting {0} in configuration file (/configuration/appSettings/add/...).", configKeyCanonicalHostName);
//#if !DEBUG
// return;
//#endif
}
context.BeginRequest += new EventHandler(context_BeginRequest);
}
#endregion
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
if (app.Request.Url.Host != this.configCanonicalHostName) //&& !app.Request.IsLocal
{
UriBuilder newUrl = new UriBuilder(app.Request.Url);
newUrl.Host = this.configCanonicalHostName;
app.Response.Redirect(newUrl.ToString(), true);
}
}
}
Then configure module in web.config
:
<?xml version="1.0"?>
<system.web>
<!-- Configuration for classic pipeline mode -->
<httpModules>
<add name="CanonicalRedirectModule" type="CanonicalRedirectModule"/>
</httpModules>
</system.web>
<system.webServer>
<!-- Configuration for integrated pipeline mode -->
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true" >
<add name="CanonicalRedirectModule" type="CanonicalRedirectModule"/>
</modules>
</system.webServer>
精彩评论