Jump to content

  • Log in with Facebook Log in with Twitter Log In with Google      Sign In   
  • Create Account

Subscribe to HRA Now!

 



SEO Class in Chicago, IL

Learn How To Optimize Your Website on July 26, 2013


Looking for personalized in-depth SEO training among your peers?



High Rankings is offering a 1-day customized SEO training class in Chicago. Class size is limited so please sign-up now if you want in!



 


Are you a Google Analytics enthusiast?

Share and download Custom Google Analytics Reports, dashboards and advanced segments--for FREE! 

 



 

 www.CustomReportSharing.com 

From the folks who brought you High Rankings!



Photo
- - - - -

301 Redirects On Windows 2000 Iis


  • Please log in to reply
25 replies to this topic

#1 AlDugan

AlDugan

    HR 5

  • Active Members
  • PipPipPipPipPip
  • 467 posts
  • Location:Keene, NH

Posted 12 January 2005 - 01:01 PM

Having trouble figuring out how to set up a 301 redirect on W2000 IIS 5. Specifically I want to redirect all variations of my home page...

CODE
http://www.mydomain.com/index.aspx
http://www.mydomain.com
http://mydomain.com
http://mydomain.com/

...to:

http://www.mydomain.com/


Since these are all obviously the exact same page when setting up the redirect I just end up in a loop.

Is there some way of accomplishing this?

#2 Raphael

Raphael

    The Limey Cowboy

  • Active Members
  • PipPipPipPipPipPip
  • 722 posts
  • Location:New England

Posted 12 January 2005 - 01:07 PM

Err, shouldn't IIS do all that automagically for you? I know apache does. You definitely shouldn't need 301 redirects for it.

#3 Shane

Shane

    HR 6

  • Active Members
  • PipPipPipPipPipPip
  • 850 posts
  • Location:Atlanta, GA

Posted 12 January 2005 - 01:52 PM

1. I think this one will have to be done in the ASPX code. Don't think you can 301 at the file level in IIS.
2. No need to worry about this one. It's treated the same as the version with the forward-slash on the end of it.
3. Set up a separate web site in IIS with a host header of "mydomain.com" and have it permanently redirect users to "www.mydomain.com"
4. Same as #2

#4 chrishirst

chrishirst

    A not so moderate moderator.

  • Moderator
  • 5,887 posts
  • Location:Blackpool UK

Posted 12 January 2005 - 01:53 PM

In ASP, Maybe I'll write a .net one some day (or maybe not biggrin.gif )
CODE
dim hostname
dim pathinfo
dim bRedirect
dim MainDomain

hostname = request.servervariables("HTTP_HOST")
pathinfo = request.servervariables("PATH_INFO")
MainDomain = "www.domain.com"
if pathinfo <> "" then
    if instr(lcase(pathinfo),"default.asp") > 0 or instr(lcase(pathinfo),"index.asp") > 0 or instr(lcase(pathinfo),"index.aspx") > 0 then
     MainDomain = MainDomain &"/" & mid(pathinfo,2,instrrev(pathinfo,"/")-1)
    else
 MainDomain = MainDomain & pathinfo
    end if
else
     MainDomain = MainDomain & "/"
end if
if left(hostname,instr(hostname,".")) <> "www." then bRedirect = true
if bRedirect then
    response.status = "301 Moved Permanently"
    response.addheader "Location", "http://" & MainDomain
    response.end
end if


put it at the top of every ASP page as an include every page and folder will be corrected.

Raphael, If IIS is left to it's own devices all the versions will return a 200 response so looks like duplicate sites (www & non-www).

#5 Shane

Shane

    HR 6

  • Active Members
  • PipPipPipPipPipPip
  • 850 posts
  • Location:Atlanta, GA

Posted 12 January 2005 - 03:05 PM

Actually, I think that would throw IIS into an infinite loop. If IIS is serving the default file in the folder, request.servervariables("PATH_INFO") will return the the name of the file even if it wasn't specified in the URL.

Example:
CODE
For http://www.mydomain.com/
request.servervariables("PATH_INFO") returns "/index.aspx"


#6 chrishirst

chrishirst

    A not so moderate moderator.

  • Moderator
  • 5,887 posts
  • Location:Blackpool UK

Posted 12 January 2005 - 04:11 PM

quite correct,

that's why the "if (instr... )" line strips off the default doc name so it doesn't cause a locked loop.

#7 Randy

Randy

    Convert Me!

  • Moderator
  • 17,540 posts

Posted 12 January 2005 - 04:26 PM

offtopic.gif

hmm... Chris I think we should probably restrict LiLo from this thread. I can hear the chanting already...

Follow the penguin - follow the penguin

#8 Shane

Shane

    HR 6

  • Active Members
  • PipPipPipPipPipPip
  • 850 posts
  • Location:Atlanta, GA

Posted 12 January 2005 - 04:44 PM

QUOTE(chrishirst @ Jan 12 2005, 04:11 PM)
that's why the "if (instr... )" line strips off the default doc name so it doesn't cause a locked loop.

That's what I'm saying. The "if (instr... )" line will always return true, even if the URL itself doesn't have the default document specified.

CODE
Example 1:
http://www.mydomain.com/index.aspx
- request.servervariables("PATH_INFO") returns /index.aspx
- instr(lcase(pathinfo),"index.aspx") > 0 is true
- So redirects to http://www.mydomain.com/
(This is assuming bredirect=true is added to that block to actually get it to redirect.  Otherwise, it only redirects if the domain doesn't start with "www.")

Example 2: http://www.mydomain.com/
- request.servervariables("PATH_INFO") returns /index.aspx
- instr(lcase(pathinfo),"index.aspx") > 0 is true
- So redirects to http://www.mydomain.com/
- We're into an infinite loop now


I actually ran the code on my IIS server to make sure.

#9 chrishirst

chrishirst

    A not so moderate moderator.

  • Moderator
  • 5,887 posts
  • Location:Blackpool UK

Posted 12 January 2005 - 05:58 PM

Well, yes and no, because the redirect will only occur when bRedirect = true and bRedirect will only be true if the hostname as requested does not contain "www"

if left(hostname,instr(hostname,".")) <> "www." then bRedirect = true

so as MainDomain is already www.domain.com and is then concatenated with pathinfo the second HTTPrequest (on redirect) will be to www.domain.com and hostname will have www in it, therefore no redirect.

the only thing I miss out is explicitly setting bRedirect to false but as it will be false by default it's a couple of clock cycles saved.

#10 Shane

Shane

    HR 6

  • Active Members
  • PipPipPipPipPipPip
  • 850 posts
  • Location:Atlanta, GA

Posted 13 January 2005 - 09:53 AM

If you don't set bRedirect to true in the instr() block, http://www.mydomain.com/index.aspx wouldn't get 301'd, so you miss 2 of his 4 cases.

#11 Raphael

Raphael

    The Limey Cowboy

  • Active Members
  • PipPipPipPipPipPip
  • 722 posts
  • Location:New England

Posted 13 January 2005 - 09:56 AM

QUOTE(chrishirst @ Jan 12 2005, 02:53 PM)
Raphael, If IIS is left to it's own devices all the versions will return a 200 response so looks like duplicate sites (www & non-www).
View Post

I'm so glad I use Apache exclusively.

#12 chrishirst

chrishirst

    A not so moderate moderator.

  • Moderator
  • 5,887 posts
  • Location:Blackpool UK

Posted 21 January 2005 - 07:07 PM

Ok thought I'd posted this but obviously not. biggrin.gif

the two variables hostname and pagename are pulled from two disconnected checks, hostname is the requested URI.
pathname is the URI that is returned by the server in response to the request and will always end with the default doc name should this be the root or a folder name.

so what the script does is strip the default doc name from the returned URI NOT the requested URI. The resultant pathinfo (minus the default doc name) is then concatenated to the domain name (MainDomain) so the redirected request will never have the default doc name in it. The fact that pathname will again return the default doc and strip it out does not matter. it is simply there to tell the useragent NOT to use the default doc name with any reference to this URI when the redirect is triggered.

So to redirect if the request includes a default doc name (which is missing from my script) an extra line should be added
CODE
if left(hostname,instr(hostname,".")) <> "www." then bRedirect = true
'  new line to be inserted
if instr(lcase(hostname),"default.asp") > 0 or instr(lcase(hostname),"index.asp") > 0 or instr(lcase(hostname),"index.aspx") > 0 then bRedirect = true
' existing code
if bRedirect then


This still cannot cause a locked loop as the second request will not meet the if criteria (no default doc name in hostname) the only time a locked loop could occur is by checking if pathname has a default doc name because this could always return true.

#13 MCP

MCP

    AKA Arizona Web, SearchStudent, and Chris Hooley

  • Active Members
  • PipPipPipPip
  • 280 posts
  • Location:Phoenix Arizona

Posted 20 February 2006 - 09:19 AM

chrishirst, do you have this script running on any of your sites? This looks like a very helpful alternative to ISAPI filters. Thanks for the code snip!

#14 jehochman

jehochman

    Jonathan Hochman

  • Active Members
  • PipPipPipPipPipPipPip
  • 1,555 posts
  • Location:Connecticut - Land of Steady Habits

Posted 24 July 2006 - 03:07 PM

For doing page level 301 redirects in IIS with non-ASP pages (e.g. hosting provider doesn't want to ASP process .HTM pages because of the possibility of script injection attacks), does anybody have a more elegant solution than ISAPI_rewrite?

...sorry for the heavy technobabbly.

#15 mcanerin

mcanerin

    HR 7

  • Active Members
  • PipPipPipPipPipPipPip
  • 2,242 posts
  • Location:Calgary, Alberta, Canada

Posted 24 July 2006 - 11:52 PM

Does this help?:

http://www.mcanerin....edirect-IIS.asp

It's even got pretty pictures and a how-to wink.gif

Also, Chris, could you do me a favour and double check the code on this page?

http://www.mcanerin....t-scripting.asp

Because I think yours looks better smile.gif If it is, I'd like to reprint it, if I may - what link/anchor text do you want for attribution/thanks/kudos?

Ian




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users