Forum Moderators: open

Message Too Old, No Replies

301 Redirect

mydomain.com to www.mydomain.com

         

tictoc

8:28 am on Jun 7, 2005 (gmt 0)

10+ Year Member Top Contributors Of The Month



In a posting last week [webmasterworld.com...]
GoogleGuy said:
For example, if you select [yourdomain.com...] as your root page, then if a spider tries to fetch [yourdomain.com...] (without the www), your web server should do a permanent (301) redirect to your root page at [yourdomain.com...]

I would like to find out the proper code to use with an .asp or any html site for this 301 redirect so that I can use www.mydomain.com when the bot comes to mydomain.com without the www in front. Can anyone point me in the right direction for this?

Jimmy Turnip

8:38 am on Jun 7, 2005 (gmt 0)

10+ Year Member



This is the asp code i use for my site. It handles querystrings too.


strServerName = Request.ServerVariables("SERVER_NAME")
strDomain = "www.example.com"
If Not strServerName = strDomain Then
strRedirectURL = "http://www.example.com" & Request.ServerVariables("URL")
If Request.ServerVariables("QUERY_STRING") <> "" Then
strRedirectURL = strRedirectURL & "?" & Request.ServerVariables("QUERY_STRING")
End If
Response.Status = "301 Moved Permanently"
Response.addheader "Location", strRedirectURL
Response.End
End If

tictoc

4:00 am on Jun 11, 2005 (gmt 0)

10+ Year Member Top Contributors Of The Month



Thanks Jimmy but where do I put this in my site? Do I make a TXT file for it or does it go on the main index page before the metatags?

g1smd

4:22 pm on Jun 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It goes in a file thepage.asp named as whatever page it is you want to redirect from.

For Windows servers also look at ISAPI rewrite and IIS rewrite.

Sidenote. With Apache it is so much easier: three lines in the .htaccess file, or a line of PHP code in visible page file.