1

I have a site, www.example.com, that I can't visit without entering the www. Using an Apache Virtual Host, I'm trying to make it so if non-www is entered, it's automatically redirected to www.

If I try going to http://www.example.com, it redirects to https://www.example.com and works

If I try going to https://www.example.com, it also works.

However, the problem is:

If I enter http://example.com, it redirects to https://example.com and says:

This site can’t be reached Check if there is a typo in example.com.
If spelling is correct, try running Windows Network Diagnostics.
DNS_PROBE_FINISHED_NXDOMAIN

I get the same message if I try going to https://example.com

The problem is my site only works with "www." On non-www, how can I force www.? I was trying the following to no avail:

<VirtualHost *:80>
    ServerName example.com
    Redirect permanent / https://www.example.com/
</VirtualHost>
<VirtualHost *:443>
    ServerName www.example.com
    ServerAlias example.com
    DocumentRoot /var/www/example.com/public/

    # Force non-www to www in HTTPS.
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

    SSLEngine On
    SSLCertificateFile /etc/letsencrypt/live/example.com/cert.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
    SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem

    ErrorLog /var/log/httpd/example.com.error.log
    CustomLog /var/log/httpd/example.com.access.log combined
</VirtualHost>

1 Answer 1

2

I don't believe this is an issue with Apache. It's more likely that your authoritative DNS simply doesn't have an entry for the non-WWW version of your web site. For instance, nslookup www.example.com will return the IP address of your server. If you do nslookup example.com it should also return the IP address of your server; if it doesn't, fix that first.

3
  • That was it! It had nothing to do with my Apache Virtual Host, but the DNS instead. After I added an A record pointing to example.com, everything worked. The redirect in my virtual host was able to redirect non-WWW to WWW and SSL. Great answer. Thanks so much for your help!
    – GTS Joe
    Nov 3, 2021 at 23:17
  • @tsc_chazz, when you mean IP, do you mean the localhost IP(127.0.0.1) or my public IP of my router? Because when I nslookup www.example.com ==> 192.64.119.109, which is namecheap's IP and nslookup example.com ==> 127.0.0.1 not even my public IP. Thank you plse
    – Laenka-Oss
    Nov 20, 2021 at 10:16
  • 1
    There are three possibilities here depending on which name server you're talking about. For your site to be visible to the world, your public address - the external address of your router - has to be set in your public authoritative DNS - you said namecheap.com? For your site to be visible to your inhouse network, its internal IP - likely 192.168.0.something - should be set in your router's local names list. And the machine itself can have the site name in its hostnames or hosts file that returns 127.0.0.1.
    – tsc_chazz
    Nov 20, 2021 at 16:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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