Need help getting Redirect/Rewrite to work

Hi, I need help with redirecting HTTP to HTTPS. I have been trying a number of different solutions from various forums but I can’t seem to get any to work and I am not sure what I am missing or forgetting to do.

Redirecting to HTTPS on computer is working fine, but I think most modern browsers attempt to redirect to HTTPS by default now anyway. The problem is on mobile, if I type in my domain without specifying HTTPS mobile browsers will just try to access the site on HTTP by default and I think the connection just times out as it produces a blank white page.

When I use whynopadlock.com the Force HTTPS status returns “Your webserver is not forcing the use of SSL.”

I have included what I hope are the nessesary files below. My VirtualHost *:80 has some of the different rewrites and redirects that I can remember trying in comments at the bottom. I also have rewrite enabled and have been restarting Apache with each change and testing against whynopadlock.

My domain is: www.unchartedpixels.co.uk
My web server is: Apache/2.4.25 (Raspbian)
The operating system my web server runs on is: Raspbian 9 (stretch)
I can login to a root shell on my machine: yes

Edit - Not sure what is happening with the code layout, will try to fix it

I have removed the comments to help reduce clutter.
000-default-le-ssl.conf
< IfModule mod_ssl.c>
< VirtualHost *:443>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/unchartedpixels.co.uk/public_html/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

ServerName unchartedpixels.co.uk
ServerAlias www.unchartedpixels.co.uk

SSLCertificateFile /etc/letsencrypt/live/unchartedpixels.co.uk/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/unchartedpixels.co.uk/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</ VirtualHost>
</ IfModule>

unchartedpixels.co.uk.conf
< VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName unchartedpixels.co.uk
ServerAlias www.unchartedpixels.co.uk

DocumentRoot /var/www/unchartedpixels.co.uk/public_html/

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



#RewriteEngine on
#RewriteCond %{SERVER_NAME} =www.unchartedpixels.co.uk [OR]
#RewriteCond %{SERVER_NAME} =unchartedpixels.co.uk
#RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

#Redirect permanent / https://www.unchartedpixels.co.uk/

RewriteEngine On
#RewriteCond %{HTTPS} !=on
#RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

RewriteCond %{HTTP_HOST} unchartedpixels\.co\.uk [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.unchartedpixels.co.uk/$1 [R,L]	
</ VirtualHost>

Apache2.conf
DefaultRuntimeDir ${APACHE_RUN_DIR}

PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5

User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}

HostnameLookups Off

ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn

IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf
Include ports.conf

<Directory />
    Options FollowSymLinks
    AllowOverride None
    Require all denied
</Directory>

<Directory /usr/share>
    AllowOverride None
    Require all granted
</Directory>

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

AccessFileName .htaccess

<FilesMatch "^\.ht">
    Require all denied
</FilesMatch>

LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

IncludeOptional conf-enabled/*.conf
IncludeOptional sites-enabled/*.conf

ServerName localhost

Hi @Chriss316

your http://www.unchartedpixels.co.uk/ doesn't answer, timeout after 20 seconds. So it's impossible to check your redirects. Same with the non-www version.

Your https - version is running.

I took a look at my router config and noticed I had my port forwarding internal and external set to 443, I changed the external to 80 and kept the internal on 443 and now on mobile HTTP seems to be redirecting to HTTPS but HTTPS is not responding

This is wrong. Your external port 80 must be redirected to your internal port 80, so that a http GET can find your webserver, the server should answer with a http 301 - redirect (not encrypted).

Now:

D:\temp>download http://www.unchartedpixels.co.uk/ -h
Error (1): Die zugrunde liegende Verbindung wurde geschlossen: Die Verbindung wurde unerwartet getrennt..
ConnectionClosed
3

305,00 milliseconds
0,31 seconds

The connection is closed, so no http-status is send.

Now it works - both domains are redirected http -> https:

Non-www:

D:\temp>download http://unchartedpixels.co.uk/ -h
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Length: 331
Content-Type: text/html; charset=iso-8859-1
Date: Tue, 09 Oct 2018 16:27:12 GMT
Location: https://www.unchartedpixels.co.uk/
Server: Apache/2.4.25 (Raspbian)

Status: 301 MovedPermanently

173,04 milliseconds
0,17 seconds

www:

D:\temp>download http://www.unchartedpixels.co.uk/ -h
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Length: 335
Content-Type: text/html; charset=iso-8859-1
Date: Tue, 09 Oct 2018 16:27:21 GMT
Location: https://www.unchartedpixels.co.uk/
Server: Apache/2.4.25 (Raspbian)

Status: 301 MovedPermanently

179,99 milliseconds
0,18 seconds

Do I need to have default-ssl.conf enabled or can I disable it. On my end when I go to HTTP://www.unchartedpixels.co.uk it wouldn’t redirect to HTTPS and the document root went to /var/www/html/ which I used on an old site I don’t use anymore instead of going to /var/www/unchartedpixels.co.uk/public_html/.

I grep’d for /var/www/html/ which I found in default-ssl.conf and changed it to /var/www/unchartedpixels.co.uk/public_html/ and now HTTP redirects to HTTPS but HTTPS is unresponsive

Edit: turns out it was 000-default.conf that was going to /var/www/html/ not default-ssl, I had forgot which was disabled and enabled, I have disabled both now and kept unchartedpixels.co.uk.conf and 000-default-le-ssl.conf enabled

I don't understand what you are doing.

There

all worked.

Now https doesn't work, http has correct 301 - redirects.

Do you use unchartedpixels.co.uk.conf, so this configuration is loaded? Did you create a backup?

I’ve been messing around with different settings for a bit. I think I might of been running into caching problems that were throwing me off.

I now have it that HTTP and HTTPS both work.

http://unchartedpixels.co.uk redirects to https://www.unchartedpixels.co.uk
http://www.unchartedpixels.co.uk stays on HTTP and doesn’t redirect so I guess I can just create a second virtual host 80 with www.etc in the ServerName to handle that?

Also I have been taking some copies of files or commenting out blocks to test with and I have a few other backup files I can template off of

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.