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!
-
"remove the trailing slash on my **domain**" - Can you be specific and give an example. If you are literally referring to the trailing slash after the hostname (ie. the "domain") then that is not possible. The trailing slash after the hostname (ie. at the start of the URL-path) is part of the HTTP spec and cannot be removed. See the following question on the Webmasters stack: https://webmasters.stackexchange.com/questions/35643/is-trailing-slash-automagically-added-on-click-of-home-page-url-in-browser – MrWhite Dec 22 '21 at 00:41
-
I'm talking about folders. – Ryder Cragie Dec 22 '21 at 01:04
-
"folders" as in physical filesystem directories? – MrWhite Dec 22 '21 at 01:06
-
Here's an example: https://RyderCragie.com/Other/Wallpapers/ – Ryder Cragie Dec 22 '21 at 01:09
-
Bear in mind that Stackoverflow hides the slash at the end. Click it and you'll see it in the address bar of your browser. That's what I want to remove. Someone posted some code below of which I have tried and it doesn't work. Here's a copy of my .htaccess file with that code: https://RyderCragie.com/htaccess – Ryder Cragie Dec 22 '21 at 01:10
-
But are you referring to physical filesystem directories or are these just arbitrary URL path segments? ie. does `/Other/Wallpapers/` exist as a physical directory on the filesystem? (Use backticks to format inline code and URLs in comments.) If these are physical directories then yes, the code in the existing answer below will not work. – MrWhite Dec 22 '21 at 01:16
-
I use FTP to upload my files to the server, so yes they will be physical directories. Although technically they're virtual and abstract. Is there any other way I can do this? All the code I've tried doesn't work. – Ryder Cragie Dec 22 '21 at 01:28
-
They can't be "physical directories" **and** "virtual and abstract" - what do you mean by that? – MrWhite Dec 22 '21 at 01:33
-
Well I can't touch them, so they're not physical. I'm messing about. In your terms, they're physical directories. – Ryder Cragie Dec 22 '21 at 01:34
-
1You should update your question with this additional information... you are not removing the "trailing slash on my domain", but "removing the trailing slash from physical filesystem directories" - which is a very different problem. – MrWhite Dec 22 '21 at 02:35
-
See https://stackoverflow.com/a/27264788/1066234 – Avatar Nov 11 '22 at 12:30
2 Answers
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.

- 43,179
- 8
- 60
- 84
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
.

- 1,151
- 5
- 7
-
My current .htaccess file can be found at https://RyderCragie.com/htaccess. Where should I place your code? – Ryder Cragie Dec 21 '21 at 23:26
-
Rename your htaccess to .htaccess and place the code provided in the answer below your line RewriteEngine On (you already have this line so remove it from the copied answer). – Ryan Dec 21 '21 at 23:58
-
It's already named .htaccess. I just copied the file and renamed it to htaccess.txt. That's the file you see at https://RyderCragie.com/htaccess. I'll add your code to .htaccess and see if it works. – Ryder Cragie Dec 22 '21 at 00:15
-
Nope. It doesn't work. I've updated the txt file as well so you can see what it looks like. Please check. – Ryder Cragie Dec 22 '21 at 00:18