Forum Moderators: open
I'm using Windows server(IIS 6.0) and I would like to redirect,
http://example.com
http://example.com/default.htm
http://www.example.com/default.htm
to
http://www.example.com
I searched for the procedure. But, I didn't get perfect solution for it.
So, Please let me know the procedure to do it.
Thanks in advance your replies.
Another option, if you have access to the IIS control panel you can set up two websites, one with the host header www.example.com and one without the www, and then redirect the non-www to the www site. (check the permanent redirect checkbox).
This won't fix the www.example.com/default.htm problem though.
For this, one option is to map the .htm extension to .asp (or .aspx) in the Application Configuration of IIS control panel, and redirect with a simple 301 script in the page itself. (make sure that default.htm is no longer you default document though)
ie. rename your home page to something else, ie. index.htm, make index.htm your default document and remove default.htm from the default document list.
Create a default.htm file and add the following code:
<%@ Language=VBScript %>
<%
Response.Status="301 Moved Permanently"
Response.AddHeader "Location", "http://www.example.com/"
%>
Or, you could upgrade to IIS7 and use the IIS7 rewrite module.
Thanks
[edited by: Ocean10000 at 12:13 am (utc) on July 22, 2009]
[edit reason] Examplified URLS [/edit]
Here are a list of basic rules that we use with ISAPI_Rewrite 2.0 and the .ini method. There is a 3.0 version using .htaccess which we use also.
[ISAPI_Rewrite]# Webresource file for ASP.NET
RewriteRule /(webresource.axd.*) /$1 [I,L]# Block external access to the httpd.ini and httpd.parse.errors files
RewriteRule /httpd(?:\.ini¦\.parse\.errors) / [F,I,O]# Block external access to the Helper ISAPI Extension
RewriteRule .*\.isrwhlp / [F,I,O]# Exclude anything that starts with _ for FrontPage extensions, if enabled.
# e.g. _derived, _private _vti_cnf
RewriteRule (.*/_.*) $1 [I,L]# Place any exceptions here.
# Example here showing to execute any images, css, js, as is.
RewriteRule /(js¦nav¦images)/(.*) /$1/$2 [I,L]
RewriteRule (.*)(\.css¦\.js) $1$2 [I,L]# Set different robots.txt for different protocol.
# You need to create a robots.https.txt (update this to point to the correct script)
RewriteCond %HTTPS on
RewriteRule /robots.txt /robots.https.txt [I,O,L]# Move anything from non- www.example.com -> www.example.com
# e.g. example.com -> www.example.com
RewriteCond %HTTPS off
RewriteCond Host: (?!^www.example.com)(.+)
RewriteRule /(.*) http\://www.example.com/$2 [I,RP]# Same as above but for SSL.
RewriteCond %HTTPS on
RewriteCond Host: (?!^www.example.com)(.+)
RewriteRule /(.*) https\://www.example.com/$2 [I,RP]# Add trailing slash for folders.
RewriteRule ([^.?]+[^.?/]) [$1$2...] [I,RP]# Convert all upper case to lower case.
# If you are using http post, you must make sure everything in the URI are lower case.
# Otherwise, the redirect will lose the form values.
RewriteCond URL ([^?]+[[:upper:]][^?]*).*
RewriteHeader X-LowerCase-URI: .* $1 [CL]RewriteCond %HTTPS on
RewriteCond X-LowerCase-URI: (.+)
RewriteRule [^?]+(.*) https\://www.example.com$1$2 [I,RP]RewriteCond %HTTPS off
RewriteCond X-LowerCase-URI: (.+)
RewriteRule [^?]+(.*) http\://www.example.com$1$2 [I,RP]# If you have any folders/files that need to be SSL enabled, list them here.
# Redirect all the following folders to SSL if it's not SSL currently.
RewriteCond %HTTPS off
RewriteRule /(client¦admin¦contact)/(.*) [example.com...] [I,RP]# This line will execute the code since it passed all the above checks and it's still SSL.
# The matching expression should be the same as last line.
RewriteCond %HTTPS on
RewriteRule /(client¦admin¦contact)/(.*) /$1/$2 [I,L]# This will force ANY https:// to http: if they are not in the last rule above.
RewriteCond %HTTPS on
RewriteRule (.*) http://www.example.com$1 [I,RP]# This will 301 all index. requests to the root level for that directory.
RewriteRule (.*/)index\.asp $1 [I,RP,L]# This will 301 old URI to new URI.
RewriteRule /sub/oldfile.htm http://www.example.com/sub/newfile.asp [I,O,RP,L]
RewriteCond Host: www\.example\.com
RewriteRule /([^?.]+)/index\.htm http\://www.exampla.com/$1/ [I,RP]
and
# This will 301 all index. requests to the root level for that directory.
RewriteRule (.*/)index\.asp $1 [I,RP,L]
RewriteCond Host: www\.example\.com
RewriteRule /([^?.]+)/index\.htm http\://www.exampla.com/$1/ [I,RP]
the above sends header 301 response and what page the 301 is going to.
RewriteRule (.*/)index\.asp $1 [I,RP,L] ]
header response 301 and not were the page is being redirected.
The first one is the best.
After much trial and error I finally found one that worked for me for an htm site. I think I tried the 1st one above and it didn't work on my server.
rewritecond %{the_request} ^[A-Z]{3,9}\ /(([^/]+/)*)index\.htm\ HTTP/
RewriteRule index\.htm$ http://www.example.com/%1 [R=301,L]\
I tired the 2nd option,
"if you have access to the IIS control panel you can set up two websites, one with the host header www.example.com and one without the www, and then redirect the non-www to the www site. (check the permanent redirect checkbox)."
But, I was not able to redirect, it was just looping. Whether I have to do anything with setting?
Then in IIS select add website add the correct IP address and the non www I always name it "example2" so I know what it is for one the path this does not matter you can select anything.
After that is set up then right click the new site you just did select homepage and select redirect check the box and add the www.example.com
[webmasterworld.com...]
More help if you need it