I have a simple question, yet I've found no posts that answer my question. I need an htaccess file that rewrites the urls so that it removes the ending /
if any, and adds .php
no matter what the folder it is in. Essentially, this is what I want to do:
http://example.com/login/ -> http://example.com/login.php
http://example.com/login -> http://example.com/login.php
http://example.com/user/test/login/ -> http://example.com/user/test/login.php
http://example.com/user/test/login -> http://example.com/user/test/login.php
http://example.com/$1/$2/$3/$4/ -> http://example.com/$1/$2/$3/$4.php
http://example.com/$1/$2/$3/$4 -> http://example.com/$1/$2/$3/$4.php
http://example.com/$1/$2/$3/ ... /$65/ -> http://example.com/$1/$2/$3/ ... /$65.php
I want it to rewrite the URL to remove the trailing slash if any, and add .php
(but do this ONLY if the file exists). And it needs to have 'wildcard' support, meaning it works for all files without having to copy the code for each individual file. That way, the URLs 'look clean'.
I've found many posts with sample .htaccess
files similar to what I want. But I haven't found one without one of these problems:
It doesn't use 'wildcard' rewrites, so I would have to copy the code for each file to make it rewrite the URL
It removes the trailing slash if any, then appends the filename to
/index.php?file=
instead of rewriting to the file.The 'wildcard' rewrite works only in the root directory. I can't use it if the files are nested like so:
/$1/$2/$3/$4/
.
Is this possible? I haven't seen anyone do it before.
Edit: I'll award a bounty if someone comes up with an .htaccess
file that does all of this correctly, but also supports URL parameters.