Redirecting domain.com to www.domain.com

Discussion in 'Apache' started by jrote1, Oct 7, 2012.

  1. #1
    I currently have this code:
         
    <VirtualHost *:80>           
           ServerName domain.com        
    
           UseCanonicalName Off        
    
           RewriteEngine on        
    
           RewriteCond %{HTTP_HOST} ^domain\.com        
           RewriteRule ^(.*)$ http://www.domain.com$1 [R=permanent,L]        
    
           LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon        
           CustomLog logs/access_log vcommon        
    
           VirtualDocumentRoot /home/%-3/public_html        
           #VirtualScriptAlias /www/hosts/%-3/cgi-bin
    </VirtualHost>
    
    Code (markup):
    But there is a couple of problems with it, it redirects all urls to www. that do not have www at the start. But I do not want to it redirect sub domains only domain.com.

    So:
    domain.com would become www.domain.com
    www.domain.com wouldnt change
    test.domain.com wouldnt change
    etc

    What would I need to do to make this work?

    Thanks in Advance
     
    Solved! View solution.
    Last edited: Oct 7, 2012
    jrote1, Oct 7, 2012 IP
  2. #2
    Try changin this two lines:
    
    RewriteCond %{HTTP_HOST} ^domain\.com        
    RewriteRule ^(.*)$ http://www.domain.com$1 [R=permanent,L]
    
    Code (markup):
    into this three lines:
    
    RewriteCond %{HTTP_HOST} ^domain\.com [NC,OR]
    RewriteCond %{HTTP_HOST} !^(.+\.)?domain\.com$ [NC]
    RewriteRule ^(.*)$ http://www.domain.com$1 [R=301,L]
    
    Code (markup):
    I haven't tested it, but I think it should work fine. Cheers!
     
    pr0t0n, Oct 7, 2012 IP
  3. jrote1

    jrote1 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It works fine but when using www.domain.com and domain.com it puts // at the end of the url. Why is it doing this ?
     
    jrote1, Oct 7, 2012 IP
  4. jrote1

    jrote1 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I have managed to fix that problem :) Also how would i redirect 403 and 404 errors to www.domain.com
     
    jrote1, Oct 7, 2012 IP
  5. pr0t0n

    pr0t0n Well-Known Member

    Messages:
    243
    Likes Received:
    10
    Best Answers:
    10
    Trophy Points:
    128
    #5
    Well, you can specify a custom 404 page using the following line in your .htaccess
    
    ErrorDocument 404 /404.html
    
    Code (markup):
    Which will return 404 status code and open that specific 404.html page (or whatever you name it). However it will not redirect it to another link, or index page. You could specify something like:

    
    ErrorDocument 404 http://www.domain.com/
    
    Code (markup):
    HOWEVER, although it will do what you wish and redirect each 404 error visitor to your index page, the server will NOT return actual 404 status, but either 200 or 302, which is not good for your SEO optimization probably. So i would suggest creating a custom error page. The same applies to 403, just replace 404 with 403 in that ErrorDocument line.

    Cheers.
     
    pr0t0n, Oct 7, 2012 IP