Linux – Trailing slash .htaccess rules causing full internal location to be used

.htaccessapache-2.2linux

This is a fairly simple problem that's been addressed all over the web, but I can't find the solution to my problem.

I'm trying to force a trailing slash on URLs on my website with this .htaccess file:

# Don't allow directory indexing
Options -Indexes -Multiviews

# Turn on the URL rewrite engine
RewriteEngine On

    # Add trailing slashes  
    RewriteCond    %{REQUEST_FILENAME}      -d
    RewriteRule    ^(.+[^/])$       $1/     [R]

    # send the request to [first pseudo-directory].php?p=[all the rest]

    # not a known file or directory
    RewriteCond     %{REQUEST_FILENAME}     !-f
    RewriteCond     %{REQUEST_FILENAME}     !-d

    #exclude URIs with a dot in them
    RewriteCond     %{REQUEST_URI}      !\..+$

    RewriteRule     ^(.*?)/(.*)$        $1.php?p=$2 [L,NS]

# Handle other errors
ErrorDocument 404 /errors.php?code=404
ErrorDocument 500 /errors.php?code=500

However, when someone visits http://domain.tld/mobile/hours (no trailing slash), it gets redirected to http://domain.tld/var/www/html/mobile/hours/ which has the trailing slash, but inexplicably also has the full internal path, which results in a 404 error, for obvious reasons.

Of course, the correct result I am looking for is http://domain.tld/mobile/hours/

Any hints?

Best Answer

For posterity:

Modifying the .htaccess file below the RewriteEngine On line to look like this:

RewriteBase /

        # Add trailing slashes
        RewriteCond    %{REQUEST_FILENAME}              !-f
        RewriteCond    %{REQUEST_URI}                   !(.*)/$
        RewriteRule    ^(.*)$                           http://sub.tld.com/$1/         [L,R=301]

...apparently fixed the issue. Not sure what I did differently this time, as I've made all these changes before, but it's working now. I think it might be related to the explicit URL and the RewriteBase / rules.

For a little more info, see this short tutorial.

Thanks for everyone's help, I'm not 100% sure what fixed this, but hopefully these lines can help someone with the same problem.