PDA

View Full Version : 301 redirects on windows server



julien_simon
03-17-2011, 06:06 PM
Hey guys,

Do you know what the best way is to do a 301 redirect from a www. version to a non www.version without using htaccess? I have a site on a windows machine.

A programmer friend suggested:
1. Convert all .htm pages to .php pages

2. Convert all internal links to point to .php pages not .htm pages

3. Add this php code to the top of every page (before the <doctype> and <html> tags):

if (substr($_SERVER['HTTP_HOST'], 0, 4) === 'www.') {

header('Location: http'.(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 's' : ''). '://' . substr($_SERVER['HTTP_HOST'], 4) . $_SERVER['REQUEST_URI']);

exit;

}

but she is not willing to do that. Moreover, she is on a windows machine so it may not even work.

Any ideas, suggestions?

Serj
03-18-2011, 02:54 PM
On a Unix machine, adding that code to every page is silly... you would just add something different to the .htaccess file:


RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ http://www.domain.com/ [R=301,L]


And this will route the domain.com and /index.html to www.domain.com

On Windows she's dealing with IIS which can certainly run PHP so adding the above should work, but best option is here: http://serverfault.com/questions/225486/best-way-to-do-a-301-redirect-from-non-www-to-www-in-iis7-5

NexusSoft
03-18-2011, 03:03 PM
Would be to modify your Web.config in the root of the application and utilize the powerful IIS rewrite rules capabilities.
Something similar to:

<rule name=”Remove WWW prefix” >
<match url=”(.*)” ignoreCase=”true” />
<conditions>
<add input=”{HTTP_HOST}” pattern=”^www\.domain\.com” />
</conditions>
<action type=”Redirect” url=”http://domain.com/{R:1}”
redirectType=”Permanent” />
</rule>
</rules></rewrite>

julien_simon
03-18-2011, 05:26 PM
thanks for the suggestions! I'll do some testing and share the results.

Tiggerito
03-18-2011, 08:57 PM
@NexusSoft suggestion requires a newer version of IIS (7 I think) and the Routing Module needs to be enabled.

If it's an older IIS then you need to install an external tool that mimics .htaccess Here's one:

http://iirf.codeplex.com/

mjtaylor
03-19-2011, 08:41 AM
Not my bailiwick, but we do have a nice sticky on this: http://www.webproworld.com/webmaster-forum/threads/65134-Canonicalization-Prevention-Guide.