I am having trouble with my Apache config and cannot get it to serve up my main webapp when I enter the URL that includes WWW (https://www.example.com) but it works fine when I don't (https://example.com).
I have tried adding the individual lines:
- ServerAlias www.example.com
- ServerAlias *example.com
to all of the VirtualHosts but that made no difference.
From my tests I see the redirects work but I can only access the main site via certain URLs:
I was wondering if someone could help me solve this so I can hit my mainsite from any URL. Also I have to demo this site to a customer very soon and am scrambling for a solution!
Here is my Apache configuration:
000-default-conf
<VirtualHost *:80>
Alias /static /home/user/project_1/static
<Directory /home/user/project_1/static>
Require all granted
</Directory>
ServerName example.com
Redirect permanent / https://example.com/
</VirtualHost>
default-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost _default_:443>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
Alias /static /var/www/example.com/static
<Directory /home/user/project_1/static>
Require all granted
</Directory>
Alias /media /home/user/project_1/media
<Directory /home/user/project_1/media>
Require all granted
</Directory>
...
...
<Directory /home/user/project_1/mywebapp>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess user python-home=/home/user/project_1/env_mywebapp python-path=/home/user/project_.......
WSGIProcessGroup mywebapp
WSGIScriptAlias / /home/user/project_1/mywebapp/wsgi.py
ServerName example.com
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
le-redirect-example.com.conf (Created by let's encrypt certbot)
<VirtualHost _default_:80>
ServerName example.com
ServerSignature Off
RewriteEngine On
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
ErrorLog /var/log/apache2/redirect.error.log
LogLevel warn
</VirtualHost>
Thank you for your help!
[UPDATE]
Here's the output from running the command "curl -I"
root@host1:~# curl -I www.example.com
HTTP/1.1 301 Moved Permanently
Date: Mon, 16 Apr 2018 18:23:20 GMT
Server: Apache/2.4.18 (Ubuntu)
Location: https://example.com/
Content-Type: text/html; charset=iso-8859-1
root@host1:~# curl -I example.com
HTTP/1.1 301 Moved Permanently
Date: Mon, 16 Apr 2018 18:24:31 GMT
Server: Apache/2.4.18 (Ubuntu)
Location: https://example.com/
Content-Type: text/html; charset=iso-8859-1
root@host1:~# curl -I https://example.com
HTTP/1.1 200 OK
Date: Mon, 16 Apr 2018 18:25:23 GMT
Server: Apache/2.4.18 (Ubuntu)
Vary: Cookie,Accept-Encoding
X-Frame-Options: SAMEORIGIN
Set-Cookie: csrftoken=zxxxxxxxxxxxxxxxxxxxxxzBcrBOvUDUdxxxxxxxxxxxxxxxx; expires=Mon, 15-Apr-2019 18:25:23 GMT; Max-Age=31449600; Path=/
Content-Type: text/html; charset=utf-8
root@host1:~# curl -I https://www.example.com
HTTP/1.1 200 OK
Date: Mon, 16 Apr 2018 18:26:15 GMT
Server: Apache/2.4.18 (Ubuntu)
Last-Modified: Tue, 03 Jan 2017 02:48:33 GMT
ETag: "1234-12347b43...."
Accept-Ranges: bytes
Content-Length: 11321
Vary: Accept-Encoding
Content-Type: text/html
You have this in your config:
This matches example.com, but not www.example.com. That's two different names. If you use https (as you do, based on the config), you also need either a certificate with multiple names, wildcard or separate certificates for example.com and www.example.com.
Use
ServerAlias www.example.com
to add an alias to the ServerName. This is also documented in the excellent apache documentation, which should be the first stop when something is not working: