So I am trying to build a clean url system in PHP to change URLS like this http://example.com/index.php?projects=05
to: http://example.com/projects/05
So far, I have figured out how to use parse_url
to map urls that look like http://example.com/index.php/projects/05
but I can't figure out how to remove the 'index.php' from the URL. Is there a way to use .htaccess to remove index.php
from the url string?
I know this is sort of a simple problem but after extensive googling, I can't find a solution.
Something like this in your .htaccess :
(Be sure the rewrite module is enabled)
I am using the following .htaccess file to remove the index.php part of the url.
Otherwise I can recommend the Kohana framework as a reference (they also have a fairly nice url parser and controller system)
You'll need to do this in Apache using mod_rewrite. You'll need to redirect all URLs to to your index.php and then, maybe using parse_url, figure out what to do with them.
For example:
The concept of decoupling the actual files/folders from the URL is called routing. Many PHP frameworks include this kind of functionality, mostly using mod_rewrite. There is a nice blog post on PHP URL Routing that implements a simple standalone router class.
It creates mapping like this:
So the requested URL results in the function
show()
of the classProjects
being called, with the parameter of1
.You can use this to build flexible mappings of pretty URLs to your PHP code.