Linode Forum
Linode Community Forums
 FAQFAQ    SearchSearch    MembersMembers      Register Register 
 LoginLogin [ Anonymous ] 
Post new topic  Reply to topic
Author Message
PostPosted: Thu Feb 18, 2010 4:33 pm 
Offline
Newbie

Joined: Thu Feb 18, 2010 4:31 pm
Posts: 3
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.


Top
   
 Post subject:
PostPosted: Thu Feb 18, 2010 4:41 pm 
Offline
Senior Newbie

Joined: Sat Sep 26, 2009 4:19 am
Posts: 14
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]


Top
   
 Post subject:
PostPosted: Thu Feb 18, 2010 9:08 pm 
Offline
Senior Member

Joined: Thu Dec 04, 2008 10:55 am
Posts: 57
Location: New Jersey
That also requires the module mod_rewrite to be loaded (it usually is with a clean install of apache).


Top
   
 Post subject:
PostPosted: Fri Feb 19, 2010 8:03 am 
Offline
Senior Member

Joined: Sun Aug 02, 2009 1:32 pm
Posts: 222
Website: https://www.barkerjr.net
Location: Connecticut, USA
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.


Top
   
 Post subject:
PostPosted: Fri Feb 19, 2010 9:13 am 
Offline
Newbie

Joined: Thu Feb 18, 2010 4:31 pm
Posts: 3
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


Top
   
 Post subject:
PostPosted: Fri Feb 19, 2010 11:53 am 
Offline
Senior Member
User avatar

Joined: Tue Nov 24, 2009 1:59 pm
Posts: 362
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.


Last edited by rsk on Fri Feb 19, 2010 12:06 pm, edited 1 time in total.

Top
   
 Post subject: How I solve the problem
PostPosted: Fri Feb 19, 2010 11:57 am 
Offline
Junior Member
User avatar

Joined: Sun Oct 02, 2005 11:54 am
Posts: 43
Website: http://www.garyscott.net
Location: Goleta, CA
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.


Top
   
 Post subject:
PostPosted: Fri Feb 19, 2010 12:11 pm 
Offline
Senior Member
User avatar

Joined: Tue Nov 24, 2009 1:59 pm
Posts: 362
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.


Top
   
 Post subject:
PostPosted: Fri Feb 19, 2010 1:17 pm 
Offline
Newbie

Joined: Thu Feb 18, 2010 4:31 pm
Posts: 3
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.


Top
   
 Post subject:
PostPosted: Wed May 09, 2012 3:12 pm 
Offline
Newbie

Joined: Wed Aug 03, 2011 5:47 am
Posts: 3
Yahoo Messenger: vpmhieu
Location: Vietnam
I have the same problem, currently when people access to www.example.com, they will be redirected to example.com automatically. But now I want to reverse it, when people come to example.com, they should be directed to www.example.com.

When I try to rewrite the url by .htaccess or change the VirtualHost file, It will lead to Loop direct error. I spent hours to figure out it, but I cannot find a solution for this issue.


Top
   
 Post subject:
PostPosted: Wed May 09, 2012 5:36 pm 
Offline
Senior Member
User avatar

Joined: Thu Nov 24, 2011 12:46 pm
Posts: 139
Location: Mesa AZ
Hard to provide any possible help there without some details of what you are doing to give a possible fix.

I prefer the mod_rewrite method myself rather doing it in the virtual hosts configuration. No server reload needed and you can provide a Perm redirect 301 code with it.

_________________
Kevin a.k.a. Dweeber


Last edited by Dweeber on Wed May 09, 2012 9:31 pm, edited 1 time in total.

Top
   
 Post subject:
PostPosted: Wed May 09, 2012 8:45 pm 
Offline
Newbie

Joined: Wed Aug 03, 2011 5:47 am
Posts: 3
Yahoo Messenger: vpmhieu
Location: Vietnam
Fortunately, I figured it out. The problem is cause by redirecting mechanism of wordpress. Just change the address in setting of wordpress, the problem is fixed.


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
RSS

Powered by phpBB® Forum Software © phpBB Group