PHP routing for friendly URLs help
I am building a website that runs all of the code trough index.php.
For example index.php?controller=something&id=01234
.
I want to use PHP to create friendly URLs, so I do this:
$request = str_replace('/root/', '', $_SERVER['REQUEST_URI']);
$params = explode('/', $request);
$controllers = array('first_c', 'second_c', 'third_c');
if (in_array($params[0], $controllers)) {
include($params[0]); // just to give an example
}
Then with a mod_rewrite rule RewriteRule ^.*$ ./index.php
I redirect everything to index.php.
I have some problems with this: Because everything is send to index.php, included .css, .js and images files. So beside the html generated by php nothing is working correctly. I'm having big troubles with this because I tried mod_rewrite for everything (instead of PHP) and I cant get it to work... please see: Apaches m开发者_运维百科od_rewrite VS. PHP routing?
Thanks.
Simply improve your mod rewrite:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(pdf|js|ico|gif|jpg|png|css|rar|zip|tar\.gz)$ index.php [L]
</IfModule>
Especially add the line RewriteCond %{REQUEST_FILENAME} !-f
And if you want more sections:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule admin/.*$ admin.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(pdf|js|ico|gif|jpg|png|css|rar|zip|tar\.gz)$ index.php [L]
</IfModule>
精彩评论