novon
novon

Reputation: 241

htaccess - forcing trailing slash two directories deep

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!

Upvotes: 1

Views: 536

Answers (3)

qmega
qmega

Reputation: 1241

DirectorySlash on

will add a trailing slash where appropriate.

Upvotes: 4

Tim Stone
Tim Stone

Reputation: 19169

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.

Upvotes: 0

nicomen
nicomen

Reputation: 1203

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)

Upvotes: 0

Related Questions