我正在构建一个通过index.php 运行所有代码的网站。
例如index.php?controller=something&id=01234
.
我想使用 PHP 创建友好的 URL,所以我这样做:
$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。
我对此有一些问题:因为所有内容都发送到index.php,包括.css、.js 和图像文件。所以除了 php 生成的 html 之外,没有任何东西可以正常工作。我在这方面遇到了很大的麻烦,因为我尝试了 mod_rewrite 来处理所有内容(而不是 PHP),但我无法让它工作......请参阅:Apache mod_rewrite VS. PHP路由? https://stackoverflow.com/questions/3075029/apaches-mod-rewrite-vs-php-routing
Thanks.
只需改进您的 mod 重写:
<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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)