用于友好URL的PHP路由帮助 [英] PHP routing for friendly URLs help
问题描述
我正在建立一个运行所有通过index.php代码的网站.
I am building a website that runs all of the code trough index.php.
例如index.php?controller=something&id=01234
.
我想使用PHP创建友好的URL,所以我这样做:
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
}
然后使用mod_rewrite规则RewriteRule ^.*$ ./index.php
我将所有内容都重定向到index.php.
Then with a mod_rewrite rule RewriteRule ^.*$ ./index.php
I redirect everything to index.php.
我对此有一些问题:因为所有内容都发送到index.php,包括.css,.js和图像文件.因此,除了由php生成的html之外,其他都无法正常工作.我对此有很大的麻烦,因为我尝试对所有内容(而不是PHP)进行mod_rewrite操作,但我无法使其正常工作...请参阅:
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 mod_rewrite VS. PHP routing?
谢谢.
推荐答案
简单地改善您的mod重写:
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>
特别添加行RewriteCond %{REQUEST_FILENAME} !-f
如果您想要更多部分,
<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>
这篇关于用于友好URL的PHP路由帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!