Question

I know how to remove slashes after url, and i know how to add it also:

# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ /$1 [L,R=301]

# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*[^/]$ /$0/ [L,R=301]

What i want is to remove all slashes so that all urls don't have slashes at the ending, but in one case the url SHOULD contain the slash.

1.) So only in this case it should add a slash:

example.com/en -> example.com/en/

2.) In any other case slash should be removed:

example.com/us/ -> example.com/us

example.com/en/product/ -> example.com/en/product

How to do that with .htaccess rules?

Was it helpful?

Solution

That's a weird question.
Anyway, you can put this code in your htaccess (which should be in root folder)

RewriteEngine on

# add trailing slash when url is /en
RewriteRule ^en$ /en/ [R=301,L]

# otherwise, remove trailing slash (except for /en/ and existing folders)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/en/$ [NC]
RewriteRule ^(.+)/$ /$1 [R=301,L]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top