Reputation: 483
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!
Upvotes: 2
Views: 132
Reputation: 16107
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.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Upvotes: 3
Reputation: 60413
You always use mod_rewrite
but you dont determine the page name in the rule(s). You jsut send it along to php which you then use to parse the proper details out of PATH_INFO based on patterns. Its called a router. I dont know of any standalone examples because they are usually pretty tied to the controller implementation.
You can check out my answer to this similar question for some implementations to look at as examples.
Upvotes: 1
Reputation: 463
You can pass some info in the url like that:
url.com/Theme/register
See this page
Upvotes: 0
Reputation: 6591
You could just define which types of requests should be passed to index.php (i.e.: register, delete etc.). If you hard-code that in htaccess, you can still use other subdirectories in your url. Or add a subdir in your htaccess to define these sort of requests like www.yoursite.com/page/register
Upvotes: 0