Welcome to WebmasterWorld Guest from 54.144.79.200

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

redirecting non www to www for multiple VHosts

Repeat the code in .htaccess for all domains?

     

ulstrup

1:44 pm on Dec 20, 2006 (gmt 0)

10+ Year Member



In this thread: [webmasterworld.com...] I found the .htaccess code to redirect non-www pages to www.pages.

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]

jdMorgan

2:33 pm on Dec 20, 2006 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



If this is a name-based virtual server, you could use %{HTTP_HOST} to do them all with one rule:

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]

Jim

ulstrup

2:48 pm on Dec 20, 2006 (gmt 0)

10+ Year Member



Great, Thanks!