1

I would like to use .htaccess to point any calls to http://example.com/ to http://www.example.com including whether a page is specified. That is, http://example.com/contact.html would redirect to to http://www.example.com/contact.html.

Can someone help with the syntax as I've tried different examples and it won't work.

TRiG
  • 10,148
  • 7
  • 57
  • 107
Andy
  • 2,991
  • 9
  • 43
  • 62

3 Answers3

0

Source

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

Trivia: If you are using an example or placeholder domain, please use example.com. This domain has been reserved specifically for this purpose in RFC2606.

orlp
  • 112,504
  • 36
  • 218
  • 315
0

You can use this, it'll work on any domain without editing:

Options +FollowSymLinks

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
CodeVirtuoso
  • 6,318
  • 12
  • 46
  • 62
0

I've written a small Apache plugin that might be helpful here, it's called "RedirToServName" and can be found in my software dump at http://www.hogyros.de/download/ . Documentation is a bit lacking; in essence all it does is generate redirects if the host name is not the canonical hostname from the ServerName directive.

Granted, using a dedicated plugin for that seems like overkill, but I find that it leaves less room for misconfiguration, as the only configuration variable is a boolean to turn it on.

Simon Richter
  • 28,572
  • 1
  • 42
  • 64