Forum Moderators: phranque

Message Too Old, No Replies

Multiple domain rewrite of non-www to www in a .htaccess file

         

Matti

4:57 pm on Sep 4, 2006 (gmt 0)

10+ Year Member



Hi. This is a common problem, with a small twist to it. I have three domains pointing to the same folder (which contains a drupal-installation). I am trying to rewrite all non-www urls to www urls using .htaccess. For a single domain (see the bold part below) everything works fine, but when I add the lines for the two other domains (the italic part below) things stop working.

RewriteEngine On
RewriteCond %{HTTP_HOST}!^www\.widget1\.info
RewriteRule (.*) [widget1.info...] [R=301,L]

RewriteCond %{HTTP_HOST}!^www\.widget2\.info
RewriteRule (.*) [widget2.info...] [R=301,L]

RewriteCond %{HTTP_HOST}!^www\.widget3\.info
RewriteRule (.*) [widget3.info...] [R=301,L]

I think I need a non-domain-specific regular expression? (The rewrite of non-www to www should executed regardless of whether the domain is widget1, widget2 or widget3.)

Unfortunately, I have no idea how to do this. Any tips would be appreciated. I've looked through many of the older posts regarding this issue, but all of them seem have been created with one specific domain in mind.

jdMorgan

6:30 pm on Sep 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's one way:

# enable rewrite engine
RewriteEngine on
# if requested hostname does NOT start with "www."
RewriteCond %{HTTP_HOST} !^www\.
# get hostname to %1 variable
RewriteCond %{HTTP_HOST} ^([a-z]+\.[a-z]+)
# prepend "www." to requested hostname and file
RewriteRule (.*) http://www.%1/$1 [R=301,L]

- or more specific -

# enable rewrite engine
RewriteEngine on
# if requested hostname does NOT start with "www."
RewriteCond %{HTTP_HOST} !^www\.
# and if one of three hostnames, then get it to %1 variable
RewriteCond %{HTTP_HOST} ^(domain1\.com¦domain2\.info¦domain3\.uk\.com)
# prepend "www." to requested hostname and file
RewriteRule (.*) http://www.%1/$1 [R=301,L]

Replace the broken pipe "¦" characters above with solid pipes before use; Posting on this forum modifies the pipe characters.

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

[edited by: jdMorgan at 6:31 pm (utc) on Sep. 4, 2006]

Matti

2:02 pm on Sep 5, 2006 (gmt 0)

10+ Year Member



Thank you! The first example works perfectly.