views:

3483

answers:

6

I'm trying to put together some htaccess code that will turn example.com/filename.php into example.com/filename/ (and force the slash) - I've tried varous approaches, but each hasn't worked quite right, from 500 errors on subfolders to issues with the trailing slash, etc...

Please help!

A: 

You could try working off the question here. Whilst the solution to the question isn't relevant (yet...) the question itself provides a set of rewrite rules which you may be able to use in your own site.

If you require symbols in URLs you could just use ".*" instead of the specific A-Za-z0-9, but if you're looking for a possible trailing slash you may want to use ".*?" instead. This is a standard regular expression feature to avoid the greediness of ".*".

Matthew Iselin
Hmm. That helped a little, and it's lead to me now using:RewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_FILENAME}\.php -fRewriteRule ^([^/]+)/$ $1.php# Forces a trailing slash to be addedRewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$RewriteRule (.*)$ /$1/ [R=301,L]This works perfectly (strips .php. and forces a trailing slash), except when in a subdirectory, when it gives a 404 error :(
!(\.[a-zA-Z0-9]{1,5}|/)$ should probably be !(([a-zA-Z0-9]/?)+)$. If I'm not mistaken that will match all alphanumeric characters followed by an (optional) "/", and match that one or more times.
Matthew Iselin
A: 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

Works like a charm - thanks for the help folks.

Is this the solution? Hasn't been selected as an answer....
Angela
A: 

Try this:

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

RewriteRule (.*)/$ $1.php [L]

The first rule redirects requests of /foo/bar.php externally to /foo/bar/. And the second rule rewrites requests of /foo/bar/ internally to /foo/bar.php.

And to force the trailing slash, try this rule:

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

This solution of Gumbo works great for files, yet it does not work with directories. In other words:

For mysite.com/file1.php, it shows mysite.com/file1/ which is great. Yet, it does not work well for directories. If I try to access the following directory (that contains index.php file inside) mysite.com/dir1, instead of showing the content of the http:/mysite.com/dir1/index.php and the url: mysite.com/dir1/, it returns 404.

My solution around it was:

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]

In other words, not to do anything if its a directory.

Hope it helps.

Another problem with the original solution is that css and the images are not loaded until I change the path to the css file and to images to absolute path.

Is there any other way to solve it, rather then changing all the paths in all the files in the website to absolute.

Thanks a lot.

Vadim
This is not an anwer to Jono Alderson’s question. Please open a new question.
Gumbo
A: 

For the path you can add the code line to your php pages.

Gentiltoutou
A: 

Hello,

I wonder if the solution by gumbo could target a specific file (buy.php for example) and ignore the rest of the php files and leave them unaltered.

Thank you, Douglas

Fisherman
Answered my own question. Now I need to get rid of the part after the slash.
Fisherman