0

I like my website to be accessible by example.org or https://example.org not www.example.org.

So I want http://www.example.org to redirect to http://example.org and http://www.example.org to https://example.org.

But an interesting thing happens:

https://www.example.org redirects to https://example.org but not http://www.example.org to http://example.org

My main.conf:

<VirtualHost *:80>
    ServerName example.org

    ServerAdmin webmaster@localhost
    DocumentRoot "/var/www/html"

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

<VirtualHost *:443>
    ServerName example.org

    ServerAdmin webmaster@localhost
    DocumentRoot "/var/www/html"

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    SSLCertificateFile /pathtocert.pem
    SSLCertificateKeyFile /pathtokey.pem
</VirtualHost>

<Directory "/var/www/html">
    Options FollowSymlinks ExecCGI
    AllowOverride None
    Require all granted
</Directory>

My www.conf:

<VirtualHost *:80>
    ServerName www.example.org
    ServerAdmin webmaster@localhost
    DocumentRoot "/var/www/html"

    RedirectPermanent / http://example.org

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

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

    ServerAdmin webmaster@localhost
    DocumentRoot "/var/www/html"

    RedirectPermanent / https://example.org

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    SSLCertificateFile /pathtocert.pem
    SSLCertificateKeyFile /pathtokey.pem
</VirtualHost>



<Directory "/var/www/html">
    Options FollowSymlinks ExecCGI
    AllowOverride None
    Require all granted
</Directory>

So what seems to be the problem? Also shouldn't both http and https redirects fail or succeed together?

peterh
  • 4,953
  • 13
  • 30
  • 44
Finch
  • 125
  • 7
  • What happens instead of the redirect? Do you have any log lines from whe it hasn't worked as it should? – Jenny D Aug 26 '17 at 19:45
  • Really, Mr Finch, I would have expected that configuring apache would be trivial to you... maybe give Root a call? – Jenny D Aug 26 '17 at 19:46
  • @JennyD Nice try Miss Shaw, I am not letting you anywhere near the server. – Finch Aug 30 '17 at 20:35

2 Answers2

1

The issue is with DNS 'A' and 'AAAA' records for the domain. There is a pre-configured A/AAAA record for www which is causing all the issues. I removed them and added a new one *.example.org. DNS redirects all wildcard subdomains to the server. The onus is now on the web server on how to deal with them.

It works fine now.

Also check this: How to redirect all wildcard subdomain to a particular subdomain? Having issues with a subdomain which runs nextcloud server

Finch
  • 125
  • 7
0

You might want to setup a rewrite, you might want something like this:

<VirtualHost *:80> RewriteEngine On # this redirect only www.example.org to https RewriteCond %{HTTPS} off RewriteCond %{HTTP_HOST} ^www.example.org [NC] RewriteCond %{HTTP_HOST} ^example.org [NC] # this redirects anything to its https equivalent RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}/$1 [R,L] </VirtualHost>

alternatively, I guess you should simply modify your redirect rule in the *:80 from RedirectPermanent / http://example.orgto RedirectPermanent / https://example.org . The redirect has to point to https, not http...

Danduk82
  • 178
  • 2
  • 9