Enforce trailing slash policy htaccess with HTTPS

293 Views Asked by At

I was wondering if someone could lend a hand with a small .htaccess issue.

Inside the .htaccess file is this for example:

Options -Indexes 
RewriteEngine On 
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} 

# Normal
RewriteRule ^about$ ./about.html [L,NC]
RewriteRule ^news$ ./news.html [L,NC]
RewriteRule ^other$ ./other.html [L,NC]

This works great if you want to enforce HTTPS and NOT have a forward slash after domain.com/about

My question is:

How do I enforce HTTPS and add the forward slash at the end like so;

https://www.domain.com/about/

or allow a user to add the forward slash at the end without it redirecting them to a 404 error page.

Also they are just HTML pages inside the main folder on the server.

Once apon a time, you would have to create folders "about" , "news", "others" etc and place an index.html file in each with all the images, css and js etc just to get the forward slash.

I hope this can be done.

Thanks!

1

There are 1 best solutions below

5
Jon Lin On

You need to add a rule to enforce the slash then change your existing rewrites so that they match against it:

Options -Indexes -Multiviews
RewriteEngine On 
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} 

# enforce the trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+[^/])$ /$1/ [R,L] 

# Normal
RewriteRule ^about/$ ./about.html [L,NC]
RewriteRule ^news/$ ./news.html [L,NC]
RewriteRule ^other/$ ./other.html [L,NC]

Also, to be on the safe side, turn off Multiviews.