Welcome to WebmasterWorld Guest from 184.73.112.180

Forum Moderators: ocean10000

How to do a 301 Redirect in IIS 7

   
7:09 pm on Mar 6, 2011 (gmt 0)

10+ Year Member



I am currently running a few websites on Windows Server 2008 using IIS 7 and PHP.

I recently logged into Google Webmaster Tools and notice that www.example.com was registering links from example.com even though I had set my Preferred domain in Google Webmaster Tools to www.example.com.

www.example.com is the version that is indexed but I do not like see example.com listed under "Links to your site" in Googleís Webmaster Tools. In the webmasters help Google suggests "You can also let everyone else know which version you prefer by doing a site-wide 301 redirect."

What is the best way to do this under IIS 7?

I want every page that comes up under example.com to redirect to the corresponding page under www.example.com. For example

example.com/blog/story.php?id=1

301 redirectís too

www.example.com/blog/story.php?id=1

Does anyone have any idea on how to do this?

Thanks for any help!

[edited by: Ocean10000 at 7:37 pm (utc) on Mar 6, 2011]
[edit reason] Switched to "example.com" Domain [/edit]

7:35 pm on Mar 6, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Here is some simple code you can put in the Global.asax, this will work for all pages processed though Asp.net.


public class Global:System.Web.HttpApplication
{
protected void Application_BeginRequest(object sender, EventArgs e)
{
string host = this.Request.Url.Host.ToLower();
if (host == "example.com")
{
host = "www.example.com";
host = this.Request.Url.AbsoluteUri.Replace("example.com", host);
}
else
{
host = this.Request.Url.AbsoluteUri;
}
if (host != this.Request.Url.AbsoluteUri)
{
this.Response.Status = "301 Moved Permanently";
this.Response.AppendHeader("Location", host);
this.Response.End();
return;
}
}
}
9:46 pm on Mar 6, 2011 (gmt 0)

10+ Year Member



But not through PHP which I use right?
9:50 pm on Mar 6, 2011 (gmt 0)

WebmasterWorld Senior Member dstiles is a WebmasterWorld Top Contributor of All Time 5+ Year Member



If you have access to IIS manager you can just add another domain record (example.com instead of www.example.com) and redirect it permanently to the www record.

If you do not have access to the server's IIS then look at using the meta canonical tag; there is info elsewhere on WebmasterWorld about this.
11:06 pm on Mar 6, 2011 (gmt 0)

WebmasterWorld Administrator 5+ Year Member Top Contributors Of The Month



If you have access to IIS manager you can just add another domain record (example.com instead of www.example.com) and redirect it permanently to the www record.


You have to be very careful with this if you are using custom URL rewrites because it may create chained redirects.

E.g. if you have:
Dynamic URL: www.example.com/somescript.php?parm=somevalue
Friendly URL: www.example.com/somefriendlyurl

Then the request for Dynamic URL should perform a 301 redirect to a friendly URL.

But if you have set up non-www domain to permanently redirects to www version via IIS admin, then the above may end up with the following chain redirects:

Request: example.com/somescript.php?parm=somevalue
301 redirect: www.example.com/somescript.php?parm=somevalue
301 redirect: www.example.com/somefriendlyurl

This is because the first of the above redirects (the one set up via IIS admin) would happen before the script is executed which would then check to see if the URL has to be redirected further.
2:19 am on Mar 7, 2011 (gmt 0)

10+ Year Member



If you have access to IIS manager you can just add another domain record (example.com instead of www.example.com) and redirect it permanently to the www record.


I have Administrator access to the server.

I am not sure what you mean by your answer. Let's say right now I have one website setup with the following bindings

www.example.com
example.com

Are you saying I need to create a separate website just for example.com and then in the HTTP redirect for the second website set it to www.example.com?

I tried this and it didnít work.

What am I missing?

Thanks
5:14 am on Mar 7, 2011 (gmt 0)

10+ Year Member



Have a look at Microsoft's URL Rewriter module for IIS7, you'll find it at iis.net and it will make short work of your redirects.
12:49 pm on Mar 7, 2011 (gmt 0)

10+ Year Member



Yeah, I just looked into URL rewrite last night to solve the problem. Here is the solution that you put in a file called web.config in the home directory of your IIS 7 website:

<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect example.com to www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="example.com" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
12:00 am on Mar 8, 2011 (gmt 0)

WebmasterWorld Senior Member dstiles is a WebmasterWorld Top Contributor of All Time 5+ Year Member



Jester - not create another web site, just another IIS record. Take the example.com from the original record, put it into the new record and point the new record permanently to www.example.com. From experience I always set up the new record to exactly the same web site as the original www, save the record then modify it to redirect - MS sometimes complains otherwise.

I agree with aakk9999 re: rewrite but since the IIS version is more basic you should not require the rewrite solution. Rewrite is always a second to direct IIS records.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month