I want to do three things on my website:
- Redirect users with a 301 to the HTTPS version of the site when they access the HTTP version
- Redirect users with a 301 to the "no-www" version of the site when they put a
www
in the url - Silently (or internally) redirect
/*
to/index.php?p=*
(as I am using a framework). (Or even, redirect "visually"/index.php?p=*
to/*
, then internally redirect/*
to/index.php?p=*
...)
Here is my current .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule (.*) https://%1/$1 [R=301]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+) index.php?p=$1 [QSA]
</IfModule>
However when I am running this configuration, my 301 redirects are not working – more precisely, a 301 code is actually sent to the client, but no Location:
header is sent:
$ curl http://mywebsite.com/path -I
HTTP/1.1 301 Moved Permanently
Date: Thu, 23 Apr 2020 19:03:57 GMT
Server: Apache/2.4.38 (Debian)
Set-Cookie: ci_session=q24j0enjrskagec2fggi256bpg5fsvs6; expires=Thu, 23-Apr-2020 20:39:57 GMT; Max-Age=7200; path=/; HttpOnly
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Content-Type: text/html; charset=UTF-8
If I try to put the block about index.php
before the two other ones, the HTTPS and www
redirection work, but the Location:
header has a weird behavior when dealing with pages:
λ curl http://mywebsite.com/fr/salon -I
HTTP/1.1 301 Moved Permanently
Date: Thu, 23 Apr 2020 19:09:03 GMT
Server: Apache/2.4.38 (Debian)
Location: https://mywebsite.com/fr/salon?p=fr/salon
Content-Type: text/html; charset=iso-8859-1
(You can also see the charset changed, for some weird reason)
What are the changes I must make to my rules to make my three redirections work?
Thank you!