rpsep2 rpsep2 - 2 months ago 12
HTML Question

htaccess why is site redirecting to //

In my htaccess I am redirecting all non https to https.
It works, but its also adding in an extra '/', so the url is
'https: //www.[MY SITE URL].co.uk//'

Why is this? To be honest, I don't really know what all this in my htaccess is doing, its copied from googling answers to 'redirect all requests to https'

My htaccess:

Options -MultiViews
RewriteEngine On # Turn on the rewriting engine

RewriteBase /

#RewriteCond %{HTTP_HOST} ^(www\.)?jobooz\.com [NC]
#RewriteRule ^(.*)$ https://www.jobooz.co.uk/$1 [R=301,L,NC]


RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI}/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}/$1

RewriteRule ^/?$ /php/index.php [NC,L] # Home page


I've also noticed any deeper urls like '/search-jobs/jobs-near-me' that I add to the url then get duplicated too, to '/search-jobs/jobs-near-me/search-jobs/jobs-near-me' when redirected to https.

All urls work fine if I go directly to the https version.

Any help appreciated, thanks.

Answer

The following rewrite rule has a problem:

RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI}/$1 [R=301,L]

The gist of why you are seeing repeated fragments in the rewritten URL is that (.*) matches everything, and that already includes the host and URI. Instead, you can try redirecting any incoming request on port 80 to HTTPS.

RewriteCond     %{SERVER_PORT} ^80$
RewriteRule     ^ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
Comments