I have the following .htaccess rule but I would only like it to run it if a file exists without the removed trailing slash:

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

I use LiteSpeed.

Also, what is the difference between the following 2 file checks? And is there any reason that they would both need to be used at the same time for 1 rule? (I currently have a few rules with both).

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME} !-f

  • What requests (URLs) you are expecting to be handled by that rule? You wouldn't necessarily expect requests that match the pattern ^(.*)/$ to map to files in the first place, so there may be something else you need to do here (rather than simply slap on a condition that checks whether the request maps to a file). Have you checked the Apache Docs (mod_rewrite introduction and RewriteCond reference)?
    – MrWhite
    Jul 26 at 14:51
  • "I would only like it to run if a file exists" - Do you understand what that rule is currently doing? What problem is it currently solving?
    – MrWhite
    Jul 26 at 14:53
  • It’s removing trailing slashes from everything except directories. (Files and invalid links). I ONLY want it to work for files. (Hence why I'm wanting a file system check to happen first before running the rule.) Jul 26 at 15:07


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.