【发布时间】:2010-11-26 16:32:59
【问题描述】:
我正在使用 CodeIgniter 运行 LAMP 环境。我希望能够使用它的 URL 模式,例如 http://localhost/controller/function/ID,但默认情况下它必须是 http://localhost/index.php/controller/function/ID。用户指南说我需要一个 .htaccess 文件来使前一种样式成为可能,并以此为例:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
但我不明白这是如何工作的。我应该把 .htaccess 放在哪里?它必须在服务器根目录中吗?
如果我的服务器上有多个站点正在运行怎么办。我是否必须将其放入/var/www/ 并根据加载的目录指定条件?或者我可以把它放在/var/www/site_using_codeigniter/ 吗?我什至不知道如何让它工作。
如果请求的 URL 实际上不存在,我是否可以让它只映射到 MVC URL?例如,http://localhost/articles/2009/some-article-name 不会是服务器上的目录,因此它会映射到 index.php 并提供给 codeigniter,但 http://localhost/forum 会 存在于服务器上但不应该存在由codeigniter处理。
我还应该对 .htaccess 文件做些什么?有什么好读的吗?
更新
我像这样更改了我的配置文件:
$config['index_page'] = ""; //used to be "index.php"
并将其添加到.htaccess 文件中:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
并将其保存在我的站点根目录中(在本例中为 /var/www/cms/)
我进入 /etc/apache2/conf.d/security(包含在 /etc/apache2/apache2.conf 中)并这样做了:
<Directory />
AllowOverride All #was "none", but this was all commented out
Order Deny,Allow
Deny from all
</Directory>
但它仍然无法正常工作。如果我导航到http://localhost/cms/index.php/hello,它会打印“Hello there”,但http://localhost/cms/hello 会给出 404 错误。
想法?
更新:成功!
我不得不深入研究我的 apache 配置文件,以最终找到所有目录的定义位置(在此之前我从未在服务器配置文件中搞乱过),并发现讨厌的 AllowOverride None 阻碍了我的伟大计划。
现在它可以完美运行了。这里的所有答案都是可行且有效的。
【问题讨论】:
标签: .htaccess codeigniter