Arnaud Bouchot
Arnaud Bouchot

Reputation: 1993

Apache Redirect all to index.php with https

I have read many answers on the subject but none is mentionning how to combine :

redirecting all traffic to index.php + redirect all http to httpS

the following works great for redirecting all to index.php :

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|public|css|js|robots\.txt)
RewriteRule ^(.*) index.php/params=$1 [L,QSA]

before I was doing this I was able to force http to https using this :

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

I just cannot find a way to mix both, so that It would redirect all traffic to index.php using the same conditions and force https.

* edit *

in case somebody else gets confused like I did.

The reason why https was systematically returning a 404 when I was calling rewritten urls is because... My domain configuration file was incomplete, my website-ssl.conf was missing the AllowOverride All directive, this is why it was all working fine until I added the url rewriting. My non-ssl was correctly setup for url-rewriting so this is why it took me a little while to realise this was not working when https.

I have then added the necessary in my /etc/apache2website-ssl.conf

<Directory /var/www/vhosts/exemple.com>
    Options -Indexes +FollowSymLinks +MultiViews
    AllowOverride All
    Require all granted
</Directory>

and this in my .htaccess

RewriteEngine On
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://exemple.com/$1 [R,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|public|css|js|robots\.txt)
RewriteRule ^(.*) index.php/params=$1 [L,QSA]
ErrorDocument 404 /index.php

I hope that'll help somebody. Thank you for your help

Upvotes: 3

Views: 2813

Answers (2)

Jaffer
Jaffer

Reputation: 2968

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}/index.php
</IfModule>

So in the above case, if user is going to http link, both(re-write to https and forward to index.php) will happen (i tried and its working). but if user is going to https directly, then you should have simple rewriting to index.php in your default-ssl

Upvotes: 1

Amit Verma
Amit Verma

Reputation: 41219

Just keep the https redirection rule above other rules :

RewriteEngine On
RewriteBase /

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [NC,L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|public|css|js|robots\.txt)
RewriteRule ^(.*) index.php/params=$1 [L,QSA]

Upvotes: 3

Related Questions