0

I'm trying to redirect my website about dentists in London from the non-www to the www version and I'm using the following code on the .htaccess of my server, but it doesn't work. What do I miss?

Thank you

RewriteEngine On
### re-direct to www
RewriteCond %{http_host} !^www.topdentists.co.uk [nc]
RewriteRule ^(.*)$ http://www.topdentists.co.uk/$1 [r=301,nc] 

2 Answers2

0

Try this:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

This is more general and will work for any site, not just the one you are currently working on. Taken from: Dense13.com (explanation of the code is in the second comment, underneath the article)

The reason why I think your code didn't work is because the RewriteCond uses a regular expression, but you didn't escape the dot (which is a regex-character).

Dimiter
  • 150
  • 7
0

To properly handle any potential https requests, as well as any strange characters, something like this is usually recommended:

# From http://stackoverflow.com/a/4958847/1078583
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
zx81
  • 38,175
  • 8
  • 76
  • 97