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 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:
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.
< IfModule mod_ssl.c>
< VirtualHost *:443>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined


SSLCertificateFile /etc/letsencrypt/live/
SSLCertificateKeyFile /etc/letsencrypt/live/
Include /etc/letsencrypt/options-ssl-apache.conf
</ VirtualHost>
</ IfModule>
< VirtualHost *:80>
ServerAdmin webmaster@localhost

DocumentRoot /var/www/

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

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

#Redirect permanent /

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

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

DefaultRuntimeDir ${APACHE_RUN_DIR}

Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5


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 /usr/share>
    AllowOverride None
    Require all granted

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

AccessFileName .htaccess

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

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 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).


D:\temp>download -h
Error (1): Die zugrunde liegende Verbindung wurde geschlossen: Die Verbindung wurde unerwartet getrennt…

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:


D:\temp>download -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
Server: Apache/2.4.25 (Raspbian)

Status: 301 MovedPermanently

173,04 milliseconds
0,17 seconds


D:\temp>download -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
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:// 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/

I grep’d for /var/www/html/ which I found in default-ssl.conf and changed it to /var/www/ 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 and 000-default-le-ssl.conf enabled


I don’t understand what you are doing.


all worked.

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

Do you use, 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. redirects to 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


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