.htaccess - 301 to non-www to www version yet still allow subdomains

Discussion in 'Apache' started by mdvaldosta, Nov 23, 2006.

  1. #1
    I've got a bit of a dillema. I'm trying to 301 redirect instances of:

    http://example.com
    http://example.com/
    http://www.example.com

    all to http://www.example.com/

    I normally use this code:
    RewriteCond %{HTTP_HOST} !^www.example\.com [NC]
    RewriteRule ^(.*) http://www.example.com/$1 [QSA,R=301,L]
    Code (markup):
    Only problem is I use subdomains on this particular site so this code don't work. Any suggestions?
     
    mdvaldosta, Nov 23, 2006 IP
  2. ArcticPro

    ArcticPro Banned

    Messages:
    1,273
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try mine its a little different:
    RewriteEngine On 
    RewriteCond %{HTTP_HOST} !^(.*)\.domain\.com$ [NC] 
    RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
    Code (markup):
     
    ArcticPro, Nov 23, 2006 IP
    1 person likes this.
  3. mdvaldosta

    mdvaldosta Peon

    Messages:
    4,079
    Likes Received:
    362
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Works great dude, thanks.
     
    mdvaldosta, Nov 24, 2006 IP