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.

11 Replies

You need a rewrite rule in your .htaccess file.

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]

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

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.

@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

````
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 Allow from all Options -MultiViews
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/

````

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.

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

## 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.

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.

@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.

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.

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.

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.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct