homepage Welcome to WebmasterWorld Guest from 54.163.139.36
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
redirecting non www to www for multiple VHosts
Repeat the code in .htaccess for all domains?
ulstrup

10+ Year Member



 
Msg#: 3194519 posted 1:44 pm on Dec 20, 2006 (gmt 0)

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

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



 
Msg#: 3194519 posted 2:33 pm on Dec 20, 2006 (gmt 0)

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

10+ Year Member



 
Msg#: 3194519 posted 2:48 pm on Dec 20, 2006 (gmt 0)

Great, Thanks!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved