Home > Back-end >  How to combine 2 redirections in one, https removing trailing slashes of url?
How to combine 2 redirections in one, https removing trailing slashes of url?

Time:08-31

My htaccess actually looks like :

RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com(.)*$ [NC]
RewriteCond %{HTTPS} !=on
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^(.*)/$ /$1 [R=301,L]

RewriteRule ^([0-9a-zA-Z/\ -] )(?:&([0-9a-zA-Z&=_\ -] ))?$ index.php?action=$1&$2 [L]
# $1 : route name and framework parameters
# $2 : classic $_GET parameters (&param=value)

It redirects to https, then redirect to url without trailing slashes, then rewrite a clean url without index etc.

I would like to know, if I enter http://www.example.com/somepage/, how to redirect to https://example.com/somepage in a single redirection instead of multiple?

CodePudding user response:

I would like to know, if I enter http://example.com/somepage/, how to redirect to https://example.com/somepage in a single redirection instead of two?

You can use this single redirect rule for that:

DirectoryIndex index.php
RewriteEngine On

# add https, remove www and remove trailing slash in same rule
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{REQUEST_URI} /$
RewriteCond %{HTTP_HOST} ^(?:www\.)?(. )$ [NC]
RewriteRule ^(.*?)/?$ https://%1/$1 [R=301,L,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .  index.php?action=$0 [L,QSA]
  • Related