www redirect, without domain.com?

Discussion in 'PHP' started by brianj, Aug 6, 2009.

  1. #1
    Hey, simple problem again.. I can only find examples with domain.com hardcoded like this:

    
    rewritecond %{http_host} ^domain.com [nc]
    rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc] 
    Code (markup):

    Does anyone know how to make the rule to generally apply for other .tld's or domains too? Just adding the www`?

    Thanks
     
    brianj, Aug 6, 2009 IP
  2. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You can try this (untested):

    
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301, L]
    
    Code (markup):
     
    premiumscripts, Aug 6, 2009 IP
  3. Sam Designs

    Sam Designs Active Member

    Messages:
    474
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Here's one I use for my website, if that helps.

    RewriteCond %{HTTP_HOST} ^followtwitter\.net$ [NC]
    RewriteRule ^(.*)$ http://www.followtwitter.net/$1 [R=301,L]
     
    Sam Designs, Aug 6, 2009 IP
  4. brianj

    brianj Member

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #4
    I've found it!

    Allright, thanks for help guys.. now i just found this code that also works for subdomains.:)

    Source: ht*p://www.remichael.de/tmp/lang/en/seo/redirect-domain-to-www-but-exclude-subdomains/
    RewriteCond %{HTTP_HOST} ^[^.]*\.[^.]*$
    RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    
    RewriteCond %{HTTP_HOST} ^www\.(.*\.[^.]*\.[^.]*)$ [NC]
    RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
    Code (markup):
     
    brianj, Aug 6, 2009 IP