1

I want the following redirects to happen:

http://www.example.org -> https://example.org [working]

https://www.example.org -> https://example.org [not working]

As you can see when using "https" it's not redirecting to https://example.org but just stays at https://www.example.org

My www.conf (for www.example.org):

<VirtualHost *:80>
    ServerName www.example.org

    Redirect / https://example.org

    ServerAdmin webmaster@localhost
    DocumentRoot /srv/www

    ErrorLog ${APACHE_LOG_DIR}/www_error.log
    CustomLog ${APACHE_LOG_DIR}/www_access.log combined

</VirtualHost>

<VirtualHost *:443>
    ServerName www.example.org

    Redirect / https://example.org

    ServerAdmin webmaster@localhost
    DocumentRoot /srv/www

    ErrorLog ${APACHE_LOG_DIR}/www_ssl_error.log
    CustomLog ${APACHE_LOG_DIR}/www_ssl_access.log combined

    Include /etc/letsencrypt/certbot.conf
</VirtualHost>

My main.conf (for example.org):

<VirtualHost *:80>
    ServerName example.org

    Redirect / https://example.org

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    ErrorLog ${APACHE_LOG_DIR}/main_error.log
    CustomLog ${APACHE_LOG_DIR}/main_access.log combined
</VirtualHost>

<VirtualHost *:443>
    ServerName example.org

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    ErrorLog ${APACHE_LOG_DIR}/main_ssl_error.log
    CustomLog ${APACHE_LOG_DIR}/main_ssl_access.log combined

    Include /etc/letsencrypt/certbot.conf
</VirtualHost>

<Directory /var/www/html>
    Options -Indexes
</Directory>
  • Do you have a VirtualHost with example.org in your config? – Gerald Schneider Oct 5 '17 at 14:59
  • Sorry for my edit, badly read your question, Gerald was faster than me to fix – yagmoth555 Oct 5 '17 at 15:01
  • @GeraldSchneider Yes, please check the op, I included it now. – Finch Oct 5 '17 at 15:06
  • Are you testing with a web browser or using a command line utility instead? Most browsers cache permanent redirects as well as HTTP Strict Transport Security headers which requires using a new incognito/anonymous browser session to test each change in the configuration that you make. - On a side note, if you're only redirecting and not serving any content you don't need to create or set a DocumentRoot in the VirtualHost entry – HBruijn Oct 5 '17 at 15:40
  • @HBruijn Yes DocumentRoot is not needed if I am just only redirecting. Needed to add it when trying to get a SSL certificate from certbot. I am testing on a web browser in incognito mode. And I am reopening the browser every time. Learnt my lessons when I messed with HSTS! – Finch Oct 5 '17 at 17:58
1

I would do this (not a complete config, just showing the concept):

<VirtualHost *:80>
    ServerName example.org
    ServerAlias www.example.org

    Redirect Permanent / https://example.org
</VirtualHost>

<VirtualHost *:443>
    ServerName example.org
    ServerAlias www.example.org

    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^example.org
    RewriteRule (.*) https://example.org$1 [R=301,L]
</VirtualHost>

The idea here is one VirtualHost per port, not per domain. www.DOMAIN and DOMAIN is almost the same. So any request that comes on port 80, it redirects to https on port 443. Any request that comes to port 443 will redirect to "itself", removing the www if needed.

Combining the VirtualHost via ServerAlias is clean and simplifies your configuration.

You need to add certificates, logs, ... as I said, this is not production ready!

  • I disabled www.conf and did as shown in your answer. Every url is redirecting to https://www.example.org/ making it even worse. I want the www subdomain gone. – Finch Oct 5 '17 at 17:53
  • Oups, my bad I answered the reverse. I modified my answer. – Nic3500 Oct 5 '17 at 18:18
  • This is totally working! :) – Finch Oct 5 '17 at 18:31

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.