I am trying to force all urls to https, www, no trailing slashes and ultimately direct all requests to a php controller page. Server uses LiteSpeed. I have tried this, but it does not seem to completely function as expected. It appears to handle trailing slash issue and https, but not the www.
# Turn on Rewrite Engine
RewriteEngine on
# Force a trailing slash except on files and directories that actually exist on server
RewriteCond %{REQUEST_URI} !(.+)/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ https://www.example.com/$1/ [R=301,L]
# Force HTTPS and WWW
RewriteCond %{HTTP_HOST} !^www.(.*)$ [OR,NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
# Redirect all requests to php controller except files and directories that actually exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
So, how can I make sure that all requests are handled by my controller page, and force url to be https, www, and add trailing slash and do so in one redirect?
2
Answers
@anubhava answer didn't work on my site. It broke links to resources, etc., but this did work with only one redirect.
You may use this code to do all redirects in a single rule and single redirect:
Make sure to clear your browser cache before testing this change.