0

I'm using IIRF on an IIS 6.0 server to redirect users from a "www" version of the site to a "non-www" version using the following code:

RewriteCond %{HTTP_HOST} ^www.mydomain.co.uk [NC] 
RewriteRule ^(.*)$ http://mydomain.co.uk/$1 [L,R=301]

However, I have to create a file with this code in the root of the website, and I have quite a lot of different websites and domains to add this to file too. Is there any way of making the code above generic, so that even though the file will still be created in each website folder (as this is easy to do with the content management system), that I don't actually have to specify the domain name and it would still redirect the user from "www" to "non-www"?

Thanks

Mark

ca8msm
  • 1,170
  • 3
  • 15
  • 37

2 Answers2

1

This is what I use...

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
robert
  • 33,242
  • 8
  • 53
  • 74
Jordano
  • 11
  • 1
0

I'm using this one:

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
DzikiMarian
  • 423
  • 3
  • 14
  • Thanks, I've tried that and one some URL's I end up with a double slash (e.g. if I go to http://www.mydomain.co.uk/mypage.html, I end up at http://mydomain.co.uk//mypage.html). Not sure if that's an issue with the redirect or something on my site though. – ca8msm Nov 15 '11 at 14:43
  • 1
    You may try removing slash before dollar sign in second line. – DzikiMarian Nov 15 '11 at 18:13
  • Correct; the slash should be captured by the (.*) in the RewriteRule, so that gets passed through $1 already and doesn't need to be explicitly specified. The line should look like `RewriteRule ^(.*)$ http://%1$1 [R=301,L]` – Doktor J Dec 12 '13 at 19:01