Home > Software engineering >  Remove Trailing Slash With .htaccess
Remove Trailing Slash With .htaccess

Time:12-23

I've looked at 5 pages of Google, and StackOverflow, and I can't seem to find a way to remove the trailing slash on my domain using .htaccess. It uses Litespeed. Can anyone provide me with code I can add to my file? I'll reply to say whether or not it worked. Thanks!

CodePudding user response:

they're physical directories.

You can't "simply" remove the trailing slash from physical filesystem directories. And I would recommend against trying to do so.

This is very different to trailing slashes on arbitrary "virtual" URLs that do not map to physical file-paths.

The trailing slash on directories is required by Apache to be able to correctly serve the DirectoryIndex document (mod_dir) and generate directory listings (mod_autoindex).

By default mod_dir explicitly appends the trailing slash to directories (if omitted) by issuing a 301 (permanent) redirect which is cached persistently by the browser.

In order to remove the trailing slash on directories (or prevent it from being appended) you need to override this default behaviour. But you must then manually append the trailing slash with an internal rewrite, so the URLs work as intended.

It is relatively trivial to prevent mod_dir appending the trailing slash:

# Prevent mod_dir from appending trailing slashes to directories
DirectorySlash Off

# Prevent mod_autoindex generating directory listings
Options -Indexes

Ensuring that directory indexes are disabled is an additional security measure when DirectorySlash Off is set, since without a trailing slash the presence of a DirectoryIndex document does not prevent mod_autoindex from generating a directory listing and exposing your file structure.

However, you now need to issue internal rewrites to "silently" append the trailing slash if a physical directory is requested without, for example:

# Append trailing slash on directories with an internal rewrite
RewriteCond $1 !/$
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (. ) $1/ [L]

Having implemented the above you should already be requesting URLs/directories without the trailing slash. However, if you are changing this behaviour and the URL with the trailing slash has been indexed by search engines or linked to by third parties then you also need to implement an external redirect to remove the trailing slash in order to preserve SEO. For simplicity, I assume you would want to remove the trailing slash from all URLs (not just physical directories).

For example, the following would need to go before the above rewrite:

# Remove trailing slash if requested directly
# NB: Applies to ALL URLs, including directories
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (. )/$ /$1 [R=301,L]

The condition that checks against the REDIRECT_STATUS environment variable is required in order to prevent a redirect loop. We only want to redirect direct requests from the client and not rewritten requests by the later rewrite.

NB: Test with 302 (temporary) redirects to avoid potential caching issues.

You will need to clear your browser (and any intermediary) caches before testing.

However, as I noted at the top of my answer, unless you have a good reason for doing this then I would recommend against implementing this as you could encounter unexpected conflicts. At the end of the day, Apache needs that trailing slash on physical filesystem directories.


Aside:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

This removes the trailing slash from an arbitrary URL-path. This is your "standard" slash removal rule. However, this will not remove the trailing slash from physical directories, as you are trying to do. For two reasons:

  • The condition explicitly checks that the request does not map to a physical directory.
  • Removing the slash from a physical directory without first setting DirectorySlash Off will result in a redirect-loop since mod_dir will naturally try to append it again.

CodePudding user response:

You can remove the trailing slash on your URL’s with the following in your .htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

For your testing, i’d recommend using R=302 so it’s not a permanent redirect as some browsers will cache these indefinitely without a full cache clear and in prod use R=301.

  • Related