How to remove trailing slashes in URLs using .htaccess

I’m having an issue trying to eliminate the trailing slash. Here’s the htaccess file as it reads now:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !^/.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/.well-known/pki-validation/(?:\ Ballot169)?
RewriteCond %{REQUEST_URI} !^/.well-known/pki-validation/[A-F0-9]{32}.txt(?:\ Sectigo\ DCV)?$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

Anyone know how to get rid of the trailing slash in websites? Many thanks.

May I ask why you want this?

Hi. Trying to get Apple Pay to recognize my site in Stripe actually. It seems to add a / slash to the end of the site address.

I can probably put together a mod_rewrite to do what you want. Won’t be back in the office until later today.

What I’d like to know first is what exactly is the rule you have included in the htaccess file above supposed to do?

I understand the redirect to https part, but the other Rewrite_Cond's don’t make a lot of sense as they don’t have any corresponding RewriteRule?

I’ve seen them automatically inserted by the host, the ‘.well-known’ folder and the associated htaccess rules are something to do with site secutity. I found this online about it:

The “ .well-known” directory will appear on all cPanel servers that are using the Comodo AutoSSL feature. The directory will contain one or more .txt files that Comodo will use to validate the domain name before it will issue an SSL Certificate for it.

If what you want to do is simply remove the trailing / from Any URL that ends with one this RedirectMatch should work. I don’t know if this will solve the problem you are having since I’ve not heard of anyone needing to do that.

RedirectMatch 302 (.*)/$ $1

Many thanks everyone for your responses. Doug, it actually turned out that the folder got loaded into the cPanel, not the file, even though it showed the file sitting there. Weird. Anyway, thank you so much for the time spent on this. Much appreciated.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.