2

This is further to the redirect query which I posted earlier in this group .

Redirect rule for Apache : Adding www to URL

It is still not working for me , so re-posting the same query . The query was to add www to a URL www.example.com.au

I tried adding both the rewrite rules one by one as shown below,but still no luck. www is getting removed from URL.

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


RewriteCond %{HTTP_HOST}   !^www\.example\.com\.au$ [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteRule ^/(.*)         http://www.example.com.au/$1 [L,R]

Please suggest further .

Zama Ques
  • 3,186
  • 12
  • 39
  • 54

2 Answers2

2
# Replace 'example.com' with your domain name
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([a-z.]+)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .? http://www.%1example.com%{REQUEST_URI} [R=301,L]
VaTo
  • 3,071
  • 3
  • 18
  • 47
1

Our webmaster appears to have solved this by having different VirtualHost for the example.com and www.example.com hostnames; the config at least has much less mod_rewrite to deal with...

<VirtualHost *:80>
    ServerName actual.example.com
    ServerAlias ...  # any other names for the host here
    DocumentRoot "/some/dir" # required by Apache?
    RedirectMatch (.*) http://www.example.com$1
</VirtualHost>

# main host (actually a CNAME for actual.example.com in DNS,
# but whatever)
<VirtualHost *:80>
    ServerName www.example.com
    ...
thrig
  • 34,333
  • 3
  • 63
  • 84