我已经安装了一个干净的Apache2 (加上PHP和MySQL)服务器,并在apache配置中启用了mod_rewrite。我添加了.htaccess文件以从url中删除index.php,如CodeIgniter维基中所述。
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]我把这个文件放在网站的根目录下。
当我尝试访问url mydomain.local/index.php/welcome时,我得到的默认页面是CodeIgniter。但是,当我尝试通过mydomain.local/welcome访问同一页面时,我得到了404页面。
如何检查整个重写规则是否有效?为什么它不起作用呢?
发布于 2011-11-25 21:19:36
我找到了解决方案。Apache配置不允许运行htaccess文件。因此,我必须将目录上的AllowOverride标志设置为AllowOverride ALL。
谢谢你的帮助!
发布于 2011-11-25 01:53:59
假设您的configuration (/application/config/config.php)禁用了index_page:
$config['index_page'] = '';你的Apache DocumentRoot是:/srv/www/
在与/application/目录相同的级别创建一个.htaccess文件,包含以下内容:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>您应该注意到,RewriteBase指向DocumentRoot (/)。如果index.php位于另一个目录中,则应相应地更改RewriteBase。
对于像这样的目录结构:
/srv/www/application/
/srv/www/system/
/srv/www/index.php
/srv/www/.htaccess您的RewriteBase应为:/
对于像这样的目录结构:
/srv/www/codeigniter/application/
/srv/www/codeigniter/system/
/srv/www/codeigniter/index.php
/srv/www/codeigniter/.htaccess您的RewriteBase应为:/codeigniter/
以此类推,你就明白了。
发布于 2013-09-19 18:01:44
默认.htaccess (来自ci用户指南http://ellislab.com/codeigniter/user-guide/general/urls.html)
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]我暂时混淆了上面的代码,但我的代码如下:
RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt|.*\.css|.*\.js|.*\.png)
RewriteRule ^(.*)$ ./index.php/$1 [L]在本地主机上,您必须在index.php前面(第三行)添加"./“,因为在我的示例中,ci目录不在根目录中。我认为"./"意味着它应该是相对路径。对于附加条件".*\.css|.*\.js|.*\.png",这意味着apache不应该重写扩展名为css、js和png的文件。
https://stackoverflow.com/questions/8259425
复制相似问题