4.4
(81)

Last Updated on November 11, 2021 by Amit

htaccess remove trailing slash

A slash , / character, that appears at the end of URL path is called a traling slash.
A typical URL with traling slash looks something like the following:

https://example.com/path/

A traling slash at the end of an existing directory prevents directory listing so its important to keep it on a directory path. Mod-dir a directory module automatically adds a slash at the end when you request a directory without a traling slash. Mod-dir does so to keep your directory secure.

You can remove traling slashes from other URLs on your server that do not point to existent folder(s) . I will show you how you can do this using RewriteRule directive.

Remove traling slash from URLs using RewriteRule

You can remove traling slashes from your URLs using RewriteRule directive either in server.config or in an htaccess file. The following example works in both context :


RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R,L]

The rule above will redirect a URL path with traling slash ie. /path/ to /path .
The RewriteCond in the rule above makes sure the rule doesn’t redirect your existing directories.

How useful was this post?

Click on a star to rate it!

Average rating 4.4 / 5. Vote count: 81

No votes so far! Be the first to rate this post.