January 3, 2017

Apache Problem (Redirects)

Apache redirect related.

I have a fresh install of Ubuntu 16.04 that I’ve used to setup my website. All DNS records are maintained through the website I bought the un-maintained server from. Everything else is done through command line by myself.

I have a main domain and a subdomain, and both have their A records pointed at the server’s IP, as well as the CName for the subdomain. I also have SSL enabled on both the main domain and subdomain as well (Self-signed).

I prefer the domain to utilize https://www protocol whenever possible, except for subdomains where I do not include the “www”. So, to do that, I have setup the apache sites-available config files to do this, instead of using .htaccess files.

It was working fine, but make a few changes, and it stopped working as fully intended.

1) Visiting domain.com/phpmyadmin refers me to https://www.domain.com/
While the redirect works well, it fails to take into account that I wanted to visit /phpmyadmin
2) Visiting https://domain.com does not redirect to include “www”
3) This might be related to the issue from #1 and #2, so hold off thinking about this until those are resolved first. But, I have a user login/registration system I built that allows users to use a “remember me” token to stay logged in after the PHP session expires. It stores the last location they were at to put them back there when (if) they are re-verified and logged back in. When they get logged back in, they go to the subdomain instead. For example, if they were supposed to visit https://www.domain.com/admin/test.php, they will instead visit https://subdomain.domain.com/admin/test.php
Once the back button is hit, and they’re already logged back in, everything works as intended.

So, here are some configuration files.

apache2.conf

Code:
<Directory /var/www/>
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>

maindomain.com.conf

Code:
<VirtualHost *:443>
        ServerName www.domain.com
        ServerAlias domain.com
        ServerAdmin user@domain.com
        DocumentRoot /var/www/domain.com

        SSLEngine On
        SSLCertificateFile /etc/letsencrypt/live/domain.com/cert.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/domain.com/privkey.pem

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

<VirtualHost *:80>
        ServerName domain.com
        RedirectMatch permanent ^(.*)$ https://www.domain.com$1
</VirtualHost>

subdomain.com.conf

Code:
<VirtualHost *:443>
        ServerName subdomain.domain.com
        ServerAlias subdomain.domain.com
        ServerAdmin user@domain.com
        DocumentRoot /var/www/subdomain.domain.com

        SSLEngine On
        SSLCertificateFile /etc/letsencrypt/live/subdomain.domain.com/cert.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/subdomain.domain.com/privkey.pem

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

<VirtualHost *:80>
        ServerName subdomain.domain.com
        Redirect permanent / https://subdomain.domain.com/
</VirtualHost>

Category : Uncategorized

Leave a Reply

Your email address will not be published. Required fields are marked *

Proudly powered by Bolkya Resha and Software Testing Theme