Forum Moderators: phranque
I have one installation of Apache2.2 and several VirtualHosts, I want all VirtualHosts to redirect non-www pages to www.
Do I repeat this code for all VirtualHosts? or is there anonther way?
.htaacess code from [webmasterworld.com...] :
Options +FollowSymLinks
RewriteEngine on
# If the requested hostname is the non-preferred one
RewriteCond %{HTTP_HOST} ^yourdomainname\.com [NC]
# then redirect to the preferred domain
RewriteRule (.*) [yourdomainname.com...] [R=301,L]
Question Example:
Options +FollowSymLinks
RewriteEngine on
# If the requested hostname is the non-preferred one
RewriteCond %{HTTP_HOST} ^yourdomainname1\.com [NC]
# then redirect to the preferred domain
RewriteRule (.*) [yourdomainname1.com...] [R=301,L]
Options +FollowSymLinks
RewriteEngine on
# If the requested hostname is the non-preferred one
RewriteCond %{HTTP_HOST} ^yourdomainname2\.com [NC]
# then redirect to the preferred domain
RewriteRule (.*) [yourdomainname2.com...] [R=301,L]
Options +FollowSymLinks
RewriteEngine on
# If the requested hostname is the non-preferred one
RewriteCond %{HTTP_HOST} ^yourdomainname3\.com [NC]
# then redirect to the preferred domain
RewriteRule (.*) [yourdomainname3.com...] [R=301,L]
In httpd.conf or conf.d:
# if hostname is not blank
RewriteCond %{HTTP_HOST} .
# and if hostname does not start with "www."
RewriteCond %{HTTP_HOST} !^www\.
# prepend "www." to hostname and redirect
RewriteRule ^/(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]