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?

Stephan Muller
  • 27,018
  • 16
  • 85
  • 126
gergokee
  • 23
  • 1
  • 7

1 Answers1

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]
Justin Iurman
  • 18,954
  • 3
  • 35
  • 54
  • 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... – gergokee Aug 01 '14 at 14:19
  • Yes ok, but what about `/us/` redirecting to `/us` in your example ? Shouldn't it be the same ? – Justin Iurman Aug 01 '14 at 15:19
  • 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 – gergokee Aug 01 '14 at 20:32