我正在使用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
阻碍了我的卓越计划。现在,它可以完美运行了。这里的所有答案都是可行且可行的。
最佳答案
在您的system/application/config/config.php
中,更改
$config['index_page'] = "index.php";
至
$config['index_page'] = "";
并在
.htaccess
中添加以下内容:RewriteEngine on
RewriteCond $1 !^(index\.php|images|stylesheets|scripts|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
将您的.htaccess文件添加到
system
文件夹所在的目录中,因此在CodeIgniter根目录中,您应该另外,由于您的服务器上运行着多个站点,因此您可能需要虚拟主机。将其添加到您拥有的每个站点的
apache2.conf
的最后一部分:Listen *:11000
<VirtualHost *:11000>
ServerAdmin you@somewhere.com
DocumentRoot "/var/www/cms"
ServerName you-dummy.com
<Directory "/var/www/cms">
AllowOverride All
Options +Indexes
DirectoryIndex index.php
Order allow,deny
Allow from all
</Directory>
ErrorLog logs/cms_log
CustomLog logs/cms_log common
</VirtualHost>
您现在可以从
http://localhost:11000/
访问站点,并使用http://localhost:11000/hello
访问 Controller 。
关于.htaccess - 如何编写.htaccess文件以使CodeIgniter URL路由正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1397014/