2

I have two kinds of links on my site: first are finishing with .html, and second that are finishing with / (with slash, in a case that filename is not finishing with .html).

Cause of some rewriting rules, in a case that file is not .html, and if is added / at the end, URL is not properly rewritten.

Like:

It is ok with link: http://mysite.com/cars/fast-cars

But not ok with link: http://mysite.com/cars/fast-cars/

So, what I need is when URL is finishing with / and not with (.html/), to be redirected to same page, without /, or in this case:

http://mysite.com/cars/fast-cars/ to be redirected to http://mysite.com/cars/fast-cars.

Hope I was clear, and that you can help me with that htaccess rule. Thank you in advance.

UPDATED: i did found part of solution here: .htaccess with or without slash.

but, also, my rule should not be valid for some subdirectories (like directory admin, orders, etc). can it be defined also with same rule?

UPDATE 2: I have rules like:

RewriteRule ^cars/fast-cars$ /seopage.php?marker=fast-cars$1

Also, tryed with rule that works:

RewriteRule (.*)/$ $1 [L,R=301]

But that rule have to be bypassed for some directories (ie. admin, orders, etc.).

Community
  • 1
  • 1
user198003
  • 11,029
  • 28
  • 94
  • 152
  • Please show your rules that you have right now (including those that you have added recently). Preferably with comments. – LazyOne Jul 15 '11 at 17:51
  • What folders (name a few)? Are they real .. or they "virtual" as the `/cars/fast-cars` is? There should be no problem with adding such condition, I'm just trying to make it clear for myself. – LazyOne Jul 15 '11 at 19:28
  • folders are real. their names (at this moment are admin and orders, but there will be more in the future). – user198003 Jul 15 '11 at 19:31

1 Answers1

5

You can do this in few ways (choose the one that works best for you).

1) Remove trailing slash / for non-existing files and folders:

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

2) Remove trailing slash / with exceptions

# remove trailing slash except some folders
RewriteCond %{REQUEST_URI} !^/(admin|orders)
RewriteRule ^(.*)/$ $1 [L,R=301]

3) You can even combine it together (which can be too much):

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

Also, consider adding this directive somewhere at the top -- documentation:

DirectorySlash Off
LazyOne
  • 158,824
  • 45
  • 388
  • 391
  • I think that 2 will do the job. Does it works for subdirs of mentioned directories? – user198003 Jul 15 '11 at 19:53
  • 1
    Yes, the condition says: "unless/except URLs **start** with `/admin` or `/orders`" which will apply to `/admin/dashboard/` etc as well. – LazyOne Jul 15 '11 at 19:59