37

When I type in my domain name like without the www (like http://example.com), it doesn't work and gives error message. However, when I add the www to it (like http://www.example.com), it works.

Isn't it supposed to work both ways (with and without the www)?

0

6 Answers 6

24

All you need is to add the following code to your root .htaccess file:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
6
  • 9
    It would be nice if you explained to people what this actually does. Here's a blog using the same code dense13.com/blog/2008/02/27/…
    – trainoasis
    Jul 16, 2014 at 7:29
  • Thanks@trainoasis for the link. It should be self explanatory. it adds www to the domain and any subdomain or subfolders and so on... Jul 16, 2014 at 15:06
  • 4
    You need to understand regular expressions to decipher the code. The second line means: if the HTTP_HOST does not (!) start with (^) "www.", then put it in memory for the next line. The next line says: from what was captured in the previous line, match everything (.*) from beginning (^) to end ($) and save everything (inside the parenthesis) as $1. Then replace $1 with "www." plus the HTTP_HOST variable. Mar 12, 2015 at 18:01
  • 1
    where to add these code tho? does it have to do with backend language? Aug 10, 2015 at 1:05
  • 1
    I tried using this code but it does not work for me. I get forbidden error when i try to access my website without www.
    – node_saini
    Apr 13, 2017 at 8:09
8

With the current version of apache, you need to do the following, which has changed compared to the previous versions:

cd /etc/apache2/sites-enabled

Now find the config file for your domain and edit it (the first three lines after <VirtualHost *:80> is what we need here):

<VirtualHost *:80>
        ServerName www.yourdomain.com
        ServerAlias yourdomain.com *.yourdomain.com
        ServerAdmin webmaster@localhost

        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>

</VirtualHost>

And now after saving changes to the file, you should reload Apache we server configuration with:

service apache2 reload

and it'll work!

P.S. You may also want not to add *.yourdomain.com to the end of your ServerAlias line.

6
  • ServerAlias www.yourdomain.com yourdomain.com works. Thanks.
    – node_saini
    Apr 13, 2017 at 8:14
  • @node_saini please upvote the answer if it helped you. :-)
    – Neeku
    Apr 14, 2017 at 9:44
  • Done :) But is this the right way to do it? It would not cause any problem right?
    – node_saini
    Apr 17, 2017 at 7:44
  • @node_saini yep! You upvote the questions and answers that you find helpful. So should do anyone else. :-)
    – Neeku
    Apr 17, 2017 at 20:18
  • It helped me no doubt but there is a right way and a wrong way to solve a problem and wrong way doesn't mean it won't work, it will work but it's not the right way. This was my only concern :)
    – node_saini
    Apr 18, 2017 at 8:23
4

to solve this issue

  • suppose you want mysubdomain.domain1.com

  • goto your dns records settings in your web panel ( cpanel , vestapanel etc ) of domain1.com

  • add there new A record with ip of your domain1.com and fill record/hostname field with @ // @ mean no www if you want www too then add another new A record and replace @ with www

  • save it

  • wait for dns changes to take place maybe take few hours

0

Alternatively, you could follow below steps which solves this problem, for sure, if you're using GoDaddy and google apps :

  1. Go to Google Apps and sign in to domain management.

  2. Then click on domain settings.

  3. Then click on domain Names.

  4. Then Click on words highlighted to set a redirect via google apps and you should see it done within 3 hours or MAX 2 DAYS (though it usually does not take more than a day in the rarest case).Just follow the simple english instructions there and you should see it done.

More details and reference :

http://www.techproceed.com/2014/05/custom-domain-setup-on-blogger-with.html

-2

You need to change any full urls to paths. If your php include or other processes are linked using urls, it will only work one way.

$.ajax(
{       
    url:'/LAYOUT/process.php'
}

as oppose to the full url

$.ajax(
{       
    url:'https://www.yourdomain.com/LAYOUT/process.php'
}

Using a simple path will allow it to work both ways with or without www.

-2

1st go your domain provider website. 2nd go dns record. 3rd add A record IP go this link

and copy 4A records and add dns.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.