HOW TO: rewrite non www to www on all addon domains and excluding sub-domains

Discussion in 'Apache' started by hajamie, Oct 16, 2005.

  1. #1
    HOW TO: rewrite non www to www on all addon domains and excluding sub-domains:

    I just spent several hours working this out, having failed to find it on the web anywhere. I figured I'd post it here. If anyone knows of any problem with using this please point it out.

    RewriteCond %{HTTP_HOST} !^www\..*
    RewriteCond %{HTTP_HOST}   !^$
    RewriteCond %{HTTP_HOST} ^([^.]*)\.(com|co\.uk)
    RewriteRule ^.*$     http://www.%1.%2%{REQUEST_URI} [R=permanent,L]
    Code (markup):
    This will rewrite all addon domains on a hosting account to www.domain.com versions if it's in the root folder. (and your hosting is setup similar to mine).
    e.g.
    domain.com => www.domain.com
    domain.co.uk => www.domain.co.uk
    domain2.com => www.domain2.com
    etc. If you want more TLDs then add them in the brackets, e.g. (com|co\.uk|ca|co\.cz)

    It also doesn't rewrite subdomains to www
    e.g.
    forums.domain.com => forums.domain.com

    Hope this helps someone.

    J.
     
    hajamie, Oct 16, 2005 IP
    mizt and forkqueue like this.
  2. sharpweb

    sharpweb Guest

    Messages:
    246
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Sweet...that might be exactly what I'm looking for. Will try it out once I get the rest of my mod rewrite code working.
     
    sharpweb, Oct 17, 2005 IP
  3. hajamie

    hajamie Peon

    Messages:
    141
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    mmm.... warm fuzzy feeling...:D
     
    hajamie, Oct 19, 2005 IP
  4. MiamiHost

    MiamiHost Peon

    Messages:
    238
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #4
    oh gawsh... I been working on something like this for days LOL

    many thanks..
     
    MiamiHost, Oct 21, 2005 IP
  5. lukasbradley

    lukasbradley Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Days? I've been working YEARS.... Fantastic post.

    I do need a little tweaking though. I just need my base url to re-direct to a full www url. In other words, if someone surfs to "http://somnia.com", I need a redirect to "http://www.somnia.com", and that's it!

    hajamie, please help? Or anyone really....
     
    lukasbradley, Oct 24, 2005 IP
  6. sharpweb

    sharpweb Guest

    Messages:
    246
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #6
    The code Hajamie wrote does that. But as a bonus if someone types http://somnia.com/folder/file.html it will redirect to http://www.somnia.com/folder/file.html

    So it should work for you too.
     
    sharpweb, Oct 24, 2005 IP
  7. lukasbradley

    lukasbradley Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I love you all in a very intense, heterosexual, you-fixed-my-problems way.

    But it doesn't seem to be working. I'm testing on my local machine, Windows XP Pro. I've added the following domains to my hosts file:

    
    127.0.0.1 jokers.com
    127.0.0.1 www.jokers.com
    
    Code (markup):
    Then the directive given above, in several places. I have also made sure to include the following:

    
    LoadModule rewrite_module modules/mod_rewrite.so
    
    Code (markup):
    Must my server name be defined as "www.jokers.com"? (I even tried that, and it still didn't work.)
     
    lukasbradley, Oct 25, 2005 IP
  8. lukasbradley

    lukasbradley Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Idiot.

    I didn't have the following:

    
    RewriteEngine On
    
    Code (markup):
    Thanks for the help.
     
    lukasbradley, Oct 26, 2005 IP
  9. MiamiHost

    MiamiHost Peon

    Messages:
    238
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I try this on another domain and it makes the page go blank, it does redirect, but blank page
     
    MiamiHost, Oct 27, 2005 IP
  10. hajamie

    hajamie Peon

    Messages:
    141
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #10
    where? what else is in your htaccess file?
     
    hajamie, Oct 27, 2005 IP
  11. MiamiHost

    MiamiHost Peon

    Messages:
    238
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #11
    looks to be just frontpage stuff

    # -FrontPage-

    IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*

    <Limit GET POST>
    order deny,allow
    deny from all
    allow from all
    </Limit>
    <Limit PUT DELETE>
    order deny,allow
    deny from all
    </Limit>
    AuthName www.*********.com
    AuthUserFile /home/sites/*********/public_html/_vti_pvt/service.pwd
    AuthGroupFile /home/sites/*********/public_html/_vti_pvt/service.grp
     
    MiamiHost, Oct 27, 2005 IP
  12. MiamiHost

    MiamiHost Peon

    Messages:
    238
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #12
    ah wait, I got it, done hard refresh on browser and now works :)
     
    MiamiHost, Oct 27, 2005 IP
  13. hajamie

    hajamie Peon

    Messages:
    141
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Sweet
    That gets me a lot, I spent ages a while ago changing things in my htaccess to get something working, only to discover I just had to hit refresh. D'oh
     
    hajamie, Oct 27, 2005 IP