Stripping trailing slash in URL doesn't work in subdirectory

The following rule works to remove the trailing slash for all pages in my web root:

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

However, it does not work when placed in a sub folder. I've also tried:

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

to no avail. What happens is, it redirects

http://example.com/testfolder/testpage/

to

http://example.com/testpage

What am I missing? Thanks in advance!

Upvotes: 2

Views: 1556

Answers (3)

Chris Tonkinson
Chris Tonkinson

Reputation: 14469

Have you tried omitting the leading slash from your replacement?

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

It's just a stone's throw from what you have. The context of the directory rewrite (being "in" /testfolder) may be the root cause of the trouble.

Upvotes: 1

Kaz
Kaz

Reputation: 58617

The trailing slash fixup is being done by mod_dir. Rewrites in your per-directory context are re-injected into the URL processing chain and subject to the fixup again.

The behavior is configurable. E.g.

<Directory /path/to/wherever>
DirectorySlash Off
...
</Directory>

The context for this is not only Directory: it is server config, virtual host, directory, .htaccess.

Upvotes: 0

Jay Mee
Jay Mee

Reputation: 55

you may be able to define this in variables also make sure Rewrite mods are enabled in your php.ini if there is no other way of doing what you need.

Upvotes: 0

Related Questions