jaccs
By:
jaccs

How can i redirect my website "example.com" to "www.example.com"

May 18, 2016 7.4k views
DigitalOcean LAMP Stack Configuration Management Deployment DNS Ubuntu

Hi,

I created a new droplet and added my existing domain from Godaddy. Later i configured my website in apache under /var/www/ with the name example. Now the problem is when i search example in google it is giving example.com as a first result. If i click that link it take me to apache home page, when i post complete URL like www.example.com it take me to my site page.

How can i redirect to www.example.com even i post example.com?

2 comments
8 Answers
This tutorial will show you how to redirect a www URL to non-www, e.g. `www.example.com` to `example.com`, with Apache on Ubuntu 14.04. We will also show you how to redirect in the other direction, from a non-www URL to www.
This tutorial will show you how to redirect a www URL to non-www, e.g. `www.example.com` to `example.com`, with Apache on Ubuntu 14.04. We will also show you how to redirect in the other direction, from a non-www URL to www.
  • Hi,
    Thanks for that.

    I followed that article and created .htaccess. The result of "curl -I http://mydomain.com" i have got below stuff.

    HTTP/1.1 200 OK
    Date: Wed, 18 May 2016 14:30:00 GMT
    Server: Apache/2.4.7 (Ubuntu)
    Last-Modified: Wed, 18 May 2016 13:29:37 GMT
    ETag: "68-5331dd9575240"
    Accept-Ranges: bytes
    Content-Length: 104
    Vary: Accept-Encoding
    Content-Type: text/html

    and still the problem persists. when i place mydomain.com it taking me to apache home page. When i place www.mydomain.com it working properly.

    But the redirection is working with.. When i place mydomain.com/index.php in url its redirecting to www.mydomain.com/index.php.

    It is not working with mydomain.com.

    However I deleted the apache home page from under /var/www/html/

    am i did wrong anywhere? (config) --- I even followed the digitalocean articles.

    • Hi,

      You should add a ServerAlias.
      In your case, check your apache2 Vhost config and look for ServerName
      Just below that, add: ServerAlias example.com (case sensitive)
      Your config is located at: /etc/apache2/sites-available/
      The reason it's going to the default page is because the apache config for your site doesn't listen to non-www.
      This is also how you can configure sub domains e.g. dev.example.com.

      • Hi,

        I already added ServerAlias in mysite.config file under /etc/apache2/sites-available/. and i added the same line in default config file also.
        like ServerAlias mysite.com

        I don't have any sub domains. am i need to add? ... for letting me to redirect for non-www.

        Regards

    • u added A record or CNAME for the www in your dns? try adding CNAME.

      • Hi,

        A big thanks to everyone who helped me to solve this redirecting issue.
        Finally i got, when i created the CNAME record with www. I destroyed the www entry for A record and created new record for CNAME with www. Its working now(Redirecting for both www & non-www).

        Thanks.

HI

I am not able to redirect my old domains abc.com & http://www.abc.com/index.aspx to

my new domain http://www.abc.com ... I have tried canonical & through ht access file but unable to do so. please assist

(sample urls) .. Please use reputationsolutions4u(dot)com to check

Answer a question...I'm ontor
new

HI
Date: Wed, 18 May 2016 14:30:00 GMT
Server: Apache/2.4.7 (Ubuntu)
Last-Modified: Wed, 18 May 2016 13:29:37 GMT
ETag: "68-5331dd9575240"
Accept-Ranges: bytes
Content-Length: 104
Vary: Accept-Encoding
Content-Type: text/html

HI

I am not able to redirect my old domains abc.com & http://www.abc.com/index.aspx to

my new domain http://www.abc.com ... I have tried canonical & through ht access file but unable to do so. please assist

(sample urls) .. Please use reputationsolutions4u(dot)com to check

Have another answer? Share your knowledge.