At the moment using .htaccess in my PHP Application to choose which page I’m rendering, so for example if the URL is url.com/register, it is actually url.com/index.php?page=register and it gets the content of /Theme/register.html.
Here it is:
RewriteEngine On
RewriteRule ^(|/)$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9/_-]+)(|)$ index.php?page=$1
RewriteRule ^(.*)\.htm$ $1.php [NC]
Thing is, I am not happy with this since if I want to use another GET parameter I’d lose this ‘pretty url’. I have seen other frameworks that do it without the need of Mod rewrite and I’d like it if someone could give me a simple and/or proper example of how to do it, or explain it.
Thanks!
The usual approach is to redirect everything* towards the main boostrap file of your application usually
index.php.In that way PHP gets the delightful job of parsing the URL and figuring out (dynamically) what page should be rendered and what parameters have been passed.
That’s where MVC routers come into place, they have the task of braking the URL into segments and figuring out which of the segments indicate that you should be using a certain controller, which of the segments indicate that the controller should be calling a certain method and which of the segments are parameters that should be passed to the method call.
This approach is used in WordPress for example and the URL parsing is done according to a URL structure you can specify in the admin.
It is also used by CakePHP an MVC framework that allows defaults to the following URL structure:
controller_name/method_name/parameter_1:value_1/parameter_2:value_2/. But can be customized to any extent such as defining a RegEx that extracts the parameters from the URL.The code bellow is the default .htaccess that WordPress generates.