How to make www work and redirect to non www

March 5, 2014 4.5k views
I was curious the best way to forward to the non www version. It seems right now www does not work at all on my site. Thanks
4 Answers
Interesting that you posted this as I am having the same exact problem. I can't access my site via www. prefix, but without it I can.
What you do is add your domain without the "www" and then create an A record with www as the hostname and then your IP address.

If you want to redirect www to without www use this code:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^$ [NC]
RewriteRule ^(.*)$$1 [R=301,L]

Or vice versa:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^$
RewriteRule (.*)$1 [R=301]

Here's a screenshot of the DNS records:
did you solve your issue?

you didn't mention yet what web server you are using because each one has different way of redirecting...

seems like you use Aapache!!

Although the other answer is not wrong (I didn't test it), I think that is a bit harder to understand. Instead of writing it there, do it in your apache virtual hosts (mine is in /etc/apache2/sites-available/000-default.conf).

The top one has all your default stuffs:
<VirtualHost *:80>
DocumentRoot ...
#More stuff

All your other urls that need to be resolved and redirected.
<VirtualHost *:80>
Redirect permanent /

I placed it all in the same file since all it does is redirect. Make sure you add one to also resolve your IP. Hope this is helpful since its the second search result on google.

Have another answer? Share your knowledge.