Delete trailing slash if not directory with apache

I have the following rewrite rules:

#remove the www.
RewriteCond %{HTTP_HOST} ^www.website.co.uk$ [NC]
RewriteRule ^(.*)$ http://local.website.co.uk/$1 [R=301,L]

#this removes php extention
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L] 

# stops you accessing url with.php
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^.?\ ]+)\.php
RewriteRule ^([^.]+)\.php(/.+)?$ /$1%{PATH_INFO} [R=301]

I want to add a rule that removes the trailing slash if someone is trying to access the site using.

eg,

website.co.uk/cheese/ should redirect to / cheese

as you can see, I have a rule that redirects ursl with the .php extension, not sure where to start.

I have a directory in the root folder that I do not want to delete the trailing url, but I can add an ignore rule for them.

Greetings

+5
source share
1 answer

Make the changes below to your .htaccess file

RewriteEngine on
RewriteBase /

#existing rule
#remove the www.
RewriteCond %{HTTP_HOST} ^www.website.co.uk$ [NC]
RewriteRule ^(.*)$ http://local.website.co.uk/$1 [R=301,L]

#new Rule
#if its not a directory
RewriteCond %{REQUEST_FILENAME} !-d
#and it has a trailing slash then redirect to URL without slash
RewriteRule ^(.+)/$ /$1 [L,R=301]

# rest of your existing rules go here
+13
source

All Articles