views:

34

answers:

3

My website structure has a root /index.php, some files as /directory/index.php and some as /directory/(filename).php

I have the following .htaccess which removes the php extensions and the "index.php" for my URLs, and forces trailing slashes on the first level directories for SEO goodness:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ $1.php [L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]

so the following are working (they show the correct page):

/
/directory/
/directory/filename/

The only thing that doesn't work, is if I type in:

/directory/filename

It goes to:

http://(mylocalurl)/Users/(myusername)/Sites/(mysitedirectory)/directory/filename/

My question is: How do I make the second level filename rewrite to force a trailing slash like:

/directory/filename/

Thanks for your help!

A: 

Not 100% sure, but it could be that it is triggered by your the second RewriteCond, the RewriteRule is run, not replacing anything, and then you have the [L] that makes it not go to the last RewriteCond.

Maybe changing the order of the two might help?

(Or else, you might want to look into Multiviews directive)

nicomen
also I noticed:"/directory/index.php"redirects to:"/directory/index/"instead of:"/directory/"any other insights would be appreciated!
novon
A: 

When you're specifying a local relative URL path to perform an external redirection on, that path needs to be prepended with a slash. Since the match to your RewriteRule test pattern will not have a leading slash, be sure to put one in the rewrite:

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ /$0/ [L,R=301]

If I've understood your problem correctly, that's the only issue you have. If you needed something else though, let me know.

Tim Stone
Thanks, I tried this, but still have the same problem:/directory/filenamedoes not redirect to:/directory/filename/
novon
@novon - And `/directory/filename.php` is an existing file? I copied your ruleset with the change, and it's working on my test server, hm..
Tim Stone
yeah I have "http://local.dev/directory/filename.php"and "http://local.dev/directory/filename/" is working"http://local.dev/directory/filename" is not
novon
@novon - So your URLs are exactly that, right (And not `http://localhost/<sitename>/directory/filename`)? I just threw it on a OS X Server, and it worked alright there too, so I'm stumped.
Tim Stone
A: 

DirectorySlash on

will add a trailing slash where appropriate.

qmega