Search code examples
mod-rewritedirectory-structuretrailing-slash

mod_rewrite, trailing slash removal, and current directory


I've perused the related questions, but I cant figure this little caveat out. The golden rule I've seen tossed around for trailing slash removal via mod_rewrite is:

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

This is all fine and good, but it strips off the preceding directory structure given one exists. So, if my application bootstrap is running at the root of the hosted path, it works, but not if in a subdirectory:

http://localhost/path/to/application/pretty/query/string/

Becomes

http://localhost/pretty/query/string (Note; slash is stripped, but so is directory)

How can I preserve the current directory location, so the previous example returns the expected:

http://localhost/path/to/application/pretty/query/string


Update

Ultimately, this is for the sake of consistency; either appending or stripping a trailing slash is a suitable. I'm working back and forth between trying to get either approach to work, with no success.

Answers that either append or strip the trailing slash are acceptable!


Solution

  • Well, I solved it for appending:

    Options +FollowSymLinks
    
    RewriteEngine On
    RewriteBase /_dev/_projects/mf_frame
    
    RewriteCond %{REQUEST_URI} !/$
    RewriteRule ^(.*)$ $1/ [R,L]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*) ?routePath=$1 [L,QSA]
    

    I was missing the RewriteBase directive. I'll have another go and stripping. Please feel free to suggest alternatives to my approach if there's something that can be done better.