Linode Forum Index Linode Forum
Linode Community Forums
 


Automatically add www in front of domain name

Click here to go to the original topic

 
       Linode Forum Index -> Linux Networking
Author Message
thehcdreamer



Joined: 18 Feb 2010
Posts: 3

Posted: Thu Feb 18, 2010 3:33 pm    Post subject: Automatically add www in front of domain name  

Hello, for a specific website I want to automatically add www in front of the domain name.

Right now it works both with www and without. I'm not sure if I have to do this on the apache side, or dns, or somewhere else.

Thanks in advance for your help.
Back to top  
shah



Joined: 26 Sep 2009
Posts: 14

Posted: Thu Feb 18, 2010 3:41 pm    Post subject:  

You need a rewrite rule in your .htaccess file.

Something along the lines of:

Code: Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Back to top  
spearson



Joined: 04 Dec 2008
Posts: 57
Location: New Jersey

Posted: Thu Feb 18, 2010 8:08 pm    Post subject:  

That also requires the module mod_rewrite to be loaded (it usually is with a clean install of apache).
Back to top  
BarkerJr



Joined: 02 Aug 2009
Posts: 220
Location: Connecticut, USA

Posted: Fri Feb 19, 2010 7:03 am    Post subject:  

Probably the cleanest way is to create separate vhosts for with and without www. One would simply contain a ServerName and a Redirect Permanent clause.
Back to top  
thehcdreamer



Joined: 18 Feb 2010
Posts: 3

Posted: Fri Feb 19, 2010 8:13 am    Post subject:  

BarkerJr wrote: Probably the cleanest way is to create separate vhosts for with and without www. One would simply contain a ServerName and a Redirect Permanent clause.

I'd like to try the vhost option. This is what I currently have:

Code:
<VirtualHost *:80>
     ServerName freestylemind.com
     ServerAlias www.freestylemind.com
     DocumentRoot /srv/www/freestylemind.com/current/public/
     ErrorLog /srv/www/freestylemind.com/shared/log/error.log
     CustomLog /srv/www/freestylemind.com/shared/log/access.log combined
     RailsEnv production
     <Directory /srv/www/freestylemind.com/current/public>
         Allow from all
         Options -MultiViews
     </Directory>
</VirtualHost>


Could you please help me to figure out how to change that only for the www, and what to put inside another virtual host just for the redirect? Thanks
Back to top  
rsk



Joined: 24 Nov 2009
Posts: 283

Posted: Fri Feb 19, 2010 10:53 am    Post subject:  

Code: <VirtualHost *:80>
     ServerName www.freestylemind.com
     DocumentRoot /srv/www/freestylemind.com/current/public/
     ErrorLog /srv/www/freestylemind.com/shared/log/error.log
     CustomLog /srv/www/freestylemind.com/shared/log/access.log combined
     RailsEnv production
     <Directory /srv/www/freestylemind.com/current/public>
         Allow from all
         Options -MultiViews
     </Directory>
</VirtualHost>
<VirtualHost *:80>
     ServerName freestylemind.com
     ErrorLog /srv/www/freestylemind.com/shared/log/error.log
     CustomLog /srv/www/freestylemind.com/shared/log/access.log combined
     RedirectPermanent / http://www.freestylemind.com/
</VirtualHost>


You'll need mod_alias enabled for Redirect calls. It is typically on in default configurations.

PS. I, personally, prefer doing it other way around, automatically cutting the www. from hostname. It's annoying, and unnecessarily increases the size of URL. But hey, however you prefer.
Back to top  
kali25



Joined: 02 Oct 2005
Posts: 43
Location: Goleta, CA

Posted: Fri Feb 19, 2010 10:57 am    Post subject: How I solve the problem  

Here is how I do it. I omitted all the other directives for clarity. But the Redirect Virtualhost block is all inclusive, no logging just the redirect.

What is nice about this redirect is it works for any page. So if the visitor goes to example.com/mydir/mypage.html, it will be redirected to www.example.com/mydir/mypage.html


Code:
## www.example.com
<VirtualHost *:80>
        ServerName www.example.com
        DocumentRoot /www/example.com/htdocs
        # Other directives omitted
</VirtualHost>



## example.com (Redirect)
<VirtualHost *:80>
        ServerName example.com
        Redirect permanent / http://www.example.com/
</VirtualHost>


Edit: I see I just posted about the same response as above. For work I redirect to www because that is how my company displays our websites on print ads, tv, and such, but for personal sites I redirect to non-www since www is not necessary, but it is nice to have www for the Ctrl-Enter crowd.
Back to top  
rsk



Joined: 24 Nov 2009
Posts: 283

Posted: Fri Feb 19, 2010 11:11 am    Post subject:  

Actually, your post reminded me of something: as the first vhost entry is being chosen when there's no Host: header present, the config as I put it would end up in a redirect loop if the client is HTTP/1.0 or simply non-compliant and doesn't supply it.

Your way of putting the redirecting vhost after the main one makes it less likely to happen. (Of course, in a multidomain vhost setup, the default vhost should be an error/information page... but in any case, having a redirect in the default isn't a good idea.)

Edited my post accordingly, swapping the blocks.
Back to top  
thehcdreamer



Joined: 18 Feb 2010
Posts: 3

Posted: Fri Feb 19, 2010 12:17 pm    Post subject:  

rsk wrote: Actually, your post reminded me of something: as the first vhost entry is being chosen when there's no Host: header present, the config as I put it would end up in a redirect loop if the client is HTTP/1.0 or simply non-compliant and doesn't supply it.

Your way of putting the redirecting vhost after the main one makes it less likely to happen. (Of course, in a multidomain vhost setup, the default vhost should be an error/information page... but in any case, having a redirect in the default isn't a good idea.)

Edited my post accordingly, swapping the blocks.

Thanks rsk, to reply to your question. I prefer to use the www for that specific site. Usually I do the other way around too. Thanks again, also thanks to kali25.
Back to top  
 
       Linode Forum Index -> Linux Networking
Page 1 of 1