I am having a little issue forcing the .php file extension to be removed in the URL.
I am successfully able to remove .php file extension if user:
#Remove PHP if original request is /foo/bar.php
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"
RewriteRule ^(.*)\.php(\?.*)?$ $1$2 [R=301,L]
My goal is to get it also to remove the extension if:
# Remove PHP if original request is /foo.php/bar
I ask because right now a user can go to the URL and type http://www.site.com/contact.php/about and it will render my about page. My goal is force the removal of the .php and render: http://www.site.com/contact/about
I was hoping to take the code I have above and add it to but I can not figure it out.
TIA
Replace your tow lines with this single one : (you have an error in your rule, that's why it is not detecting .php in the middle and you don't need the rewrite condition)
the following .htaccess gives me the requested parameters, you can get "page"
Get "page" parameter and then call it like this
and remember to remove
.php
ext from your linksIt looks like you got the removing part, but you're missing the internally rewriting part. What you have attempts to remove the
php
out of the URL and redirects the client to a URL without it. But your condition isn't matching requests, change it to:Then you need to internally rewrite it back (don't redirect browser). So in the same htaccess file, add:
My solution for these problems is to basically avoid using complex rewrite rules and do URL routing from the php side, via a simple front controller.
Write the following .htaccess file at the root of your website:
Then write an index.php file in the same directory. In the index.php file, you can still get the whole URL information, and choose a PHP file to include based on this.
That example is extremely basic, and I am probably ignoring subtleties of your website's architecture, but this approach is much more viable in the long run, because you can really map any URL of your liking to PHP scripts without having to endure the complexity of mod_rewrite.
This is the pattern that has been adopted by virtually every existing PHP framework (at least the MVC ones).
An minimalist example of this approach can be found in the Slim micro framework: http://www.slimframework.com/