maiconfz
By:
maiconfz

How can I redirect www.mysite.com to mysite.com using DigitalOcean DNS?

December 18, 2012 21.8k views
I've tried to use cname, A record, and I didn't get it to work.
1 comment
7 Answers
Create CNAME record:
www
mysite.com.
Tried htacess ?

http://www.scriptalicious.com/blog/2009/04/redirecting-www-to-non-www-using-htaccess/
We've done this redirect multiple times and it's always been through a redirect on the web service layer whether that's in Apache or Nginx.

In Nginx you can do this very simply:
server {
listen 80;
server_name mysite.com;
return 301 https://www.mysite.com$request_uri;
}

If you are using Apache let us know.
  • What need to be done in Apache?

    On the .htaccess I have used :

    <IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
    Options -MultiViews
    </IfModule>

    RewriteEngine On
    
    # Redirect Trailing Slashes...
    #RewriteRule ^(.*)/$ /$1 [L,R=301]
    
    # Redirect all non-www requests to your site to the www version
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
    
    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
    

    </IfModule>

    But non-www wouldn't redirect to www

nightflash is correct. 'www' pointing to 'mysite.com.' is the right way to do it in Digital Ocean's DNS settings. Don't forget the '.' at the end of your domain.
About how long does it take for this to propagate? I still only see the default apache page at www.mysite.com whereas mysite.com has been fine. Do I need to restart the server?
@david: Apache config changes take effect instantly after restarting Apache (assuming your DNS records are proper). You need to add a VirtualHost as follows:
<VirtualHost *:80>

ServerName www.example.com
Redirect permanent / http://example.com/
</VirtualHost>

Ubuntu 12.04: https://www.digitalocean.com/community/articles/how-to-set-up-apache-virtual-hosts-on-ubuntu-12-04-lts.

Ubuntu 14.04: https://www.digitalocean.com/community/articles/how-to-set-up-apache-virtual-hosts-on-ubuntu-14-04-lts.
by Etel Sverdlov
This tutorial shows you how to set up a new directory, how to grant the correct permissions, how to add content to a new page, how to turn on virtual hosting, and how to, if needed, set up local hosts. Virtual Hosts are used to run more than one domain off of a single IP address and allows you to run multiple sites off of a single DigitalOcean droplet. This tutorial describes how to set up virtual hosts with Apache.

Sorry to re-open this thread..

I need to redirect: mail.mydomain.com to other URL google.com/mail

I'm using Ngix, how can I do this? May I do this in DNS or in nginx config?

Cheers.

  • You can do that by adding an nginx server block as follows:

    server {
      listen 80;
      server_name mail.domain.com;
    
      return 301 https://google.com/mail;
    }
    

    Make sure you replace the bits in red to suit your needs.

Have another answer? Share your knowledge.