Apache's mod_rewrite or Digital Ocean's DNS Records?

May 17, 2013 3.7k views
I was trying to figure out how to redirect a website such that www.example.com will lead to example.com instead of giving me a 404 error when I saw this Apache guide: http://httpd.apache.org/docs/2.4/rewrite/remapping.html I am wondering will this be better than using the DNS records in Digital Ocean's GUI, considering that the DNS records are still not working despite setting them up last night (They should have propagated by now...) Thanks!
3 Answers
You have to have DNS records for your subdomains to resolve to your droplet. You should also configure apache properly to redirect from non-www to www (or from www to non-www).
Make sure you point the IP of the server to your dns records.

@ A xxx.xxx.xxx.xx
www CNAME @

www A xxx.xxx.xxx.xx

Add the following to your .htaccess.

# From www to non-www

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]

From non-www to www
# Force www in HTTP
RewriteEngine On
# line below is optional
# RewriteCond %{REQUEST_URI} !^/(blog|blog.domain.com)
RewriteCond %{HTTP_HOST} ^domain.com
# RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
Ah I see; I thought having only one of them works :X
Have another answer? Share your knowledge.