Automatically add www in front of 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.
11 Replies
Something along the lines of:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
@BarkerJr:
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:
<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
````
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.
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
<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.
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.
@rsk:
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.
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.
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.