homepage Welcome to WebmasterWorld Guest from 54.166.111.111
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Microsoft / Microsoft IIS Web Server and ASP.NET
Forum Library, Charter, Moderators: ocean10000

Microsoft IIS Web Server and ASP.NET Forum

    
How to do a 301 Redirect in IIS 7
JesterMagic




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

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]

 

Ocean10000




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

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;
}
}
}

JesterMagic




msg:4277563
 9:46 pm on Mar 6, 2011 (gmt 0)

But not through PHP which I use right?

dstiles




msg:4277564
 9:50 pm on Mar 6, 2011 (gmt 0)

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.

aakk9999




msg:4277570
 11:06 pm on Mar 6, 2011 (gmt 0)

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.

JesterMagic




msg:4277611
 2:19 am on Mar 7, 2011 (gmt 0)

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

foghead




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

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.

JesterMagic




msg:4277757
 12:49 pm on Mar 7, 2011 (gmt 0)

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>

dstiles




msg:4278075
 12:00 am on Mar 8, 2011 (gmt 0)

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Microsoft / Microsoft IIS Web Server and ASP.NET
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved