Score:1

Apache Not Redirecting Non-WWW to WWW HTTPS

us flag

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>
Score:2
vn flag

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.

us flag
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!
Laenka-Oss avatar
lk flag
@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
tsc_chazz avatar
vn flag
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`.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.