1

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?

1 Answer 1

1

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] 
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, it works ! Reason is that with preserving the slash when /en/ is in URL, is that i want to highlight that it is the base folder for the english language. Other pages are just sub pages...
Yes ok, but what about /us/ redirecting to /us in your example ? Shouldn't it be the same ?
yes, well in my case i have two languages one is the main domain(default), the other is english /en/ so if i add another language, i will do as with english in .htaccess

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.