Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
    
What's the <VirtualHost> configuration look like? Any maybe see what you get from a RewriteLog with RewriteLogLevel turned up to 9? –  Shane Madden Dec 15 '11 at 4:49

3 Answers 3

up vote 1 down vote accepted

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.

share|improve this answer

Try changing this Rule:

RewriteRule    ^(.+[^/])$       $1/     [R]

To include a slash before your backreference:

RewriteRule    ^(.+[^/])$       /$1/     [R]
share|improve this answer
    
Unfortunately, this resulted in no change whatsoever. –  rockerest Dec 14 '11 at 23:55
    
I wonder if this is conflicting with mod_dir's DirectorySlash (which is usually turned on by default). Maybe turning DirectorySlash off might help? –  Jon Lin Dec 14 '11 at 23:58
    
I added DirectorySlash off to the top of the .htaccess file, and it didn't change the behavior at all. On another note, I've created this question on Stack Overflow because I think the issue might be related to jQuery Mobile. –  rockerest Dec 15 '11 at 0:02

Add the L flag as strongly recommended by the documentation like

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

It will make the evaluation of the rules stop at this point and emit the redirect with the then-current rewritten URL.

If you omit the L following rules will still be execute and further rewrites will occur with the final rewrite being from the URL to the filesystem path. Only then, the actual redirect would occur with the then final path that you can observe in your current response.

share|improve this answer
    
This didn't change the result. –  rockerest Dec 15 '11 at 0:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.