如何编写.htaccess文件以使CodeIgniters URL路由工作?

如何编写.htaccess文件以使CodeIgniters URL路由工作?,.htaccess,codeigniter,.htaccess,Codeigniter,我用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) Rewr

我用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/
并根据加载的目录指定条件?或者我可以使用codeigniter将其放入
/var/www/site\u吗?我甚至不知道如何让它工作

如果请求的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>

AllowOverride All为“无”,但这一切都被注释掉了
命令拒绝,允许
全盘否定
然而,它仍然不起作用。如果我导航到
http://localhost/cms/index.php/hello
,它会打印“你好”,但是
http://localhost/cms/hello
给出一个404错误

想法

更新:græt成功! 我不得不翻阅我的apache配置文件,最终找到所有目录的定义位置(在此日期之前我从未在服务器配置文件中乱搞过),并发现讨厌的
AllowOverride None
阻碍了我的伟大计划


现在它工作得很好。这里的所有答案都是可行的和有效的。

将其放在网站的文档根目录下

如果您有多个站点,请将其放在每个此类站点的顶部

如果有多个站点从同一目录运行,则可以使用“重写条件”将特定站点排除或包括在“重写规则”中


您可以用同样的方法排除某些目录(例如/文章)。

系统/application/config/config.php中,更改

$config['index_page'] = "index.php";

并在
.htaccess
中添加以下内容:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|stylesheets|scripts|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
将.htaccess文件添加到
system
文件夹所在的同一目录中,因此在CodeIgniter根目录中,您应该具有

  • 系统/
  • 用户指南/
  • index.php
  • .htaccess
  • license.txt
此外,由于您的服务器上运行着多个站点,因此您可能需要虚拟主机。对于您拥有的每个站点,将此添加到您的
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>
Listen*:11000
服务器管理员you@somewhere.com
DocumentRoot“/var/www/cms”
ServerName you-dummy.com
允许超越所有
选项+索引
DirectoryIndex.php
命令允许,拒绝
通融
错误日志/cms\U日志
自定义日志/cms\U日志通用

您现在可以从
http://localhost:11000/
并使用
http://localhost:11000/hello

此特定的.htaccess文件看起来需要放在站点的根目录中(即当前正在执行的站点)

查看以了解您可以指定的其他类型的条件。基本上,
RewriteRule
只有在其前面的所有
RewriteCond
都匹配时才会应用。如果请求的URL不存在,您可以尝试将这些规则添加到重定向:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
mod_rewrite功能非常强大,但如果它没有做到您认为应该做的事情,它可能会是一个真正的痛苦。我也不知道如何调试它的指令

就您可以使用.htaccess文件做的其他事情而言,始终是一个很好的起点。最常用的是每个目录的身份验证。大多数服务器配置指令都在.htaccess文件中受支持(前提是服务器的说明是这样的),但有些指令的语法不同。


<IfModule rewrite_module>
RewriteEngine on
RewriteBase /

# Removes the "/" at the end
RewriteRule (.+)/$ /$1 [L,R]

RewriteCond $1 !^(index\.php|dhtml|img|css|js|favicon\.ico|robots\.txt)
RewriteRule ^([^?&]+) /index.php/$1 [L]
</IfModule>
重新启动发动机 重写基/ #删除结尾处的“/” 重写规则(+)/$/$1[L,R] 1美元^(索引\.php | dhtml | img | css | js | favicon\.ico | robots\.txt) 重写规则^([^?&]+)/index.php/$1[L]
在这里尝试了答案,但对我无效。所以我尝试了以下方法,它做到了

在根目录中创建.htaccess,其中index.php和license.txt是可用的。在文件中输入以下代码并将其存储:

DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]  
完成了!试试看,它肯定会有用的。
希望能有所帮助

在找到更改“AllowOverRide All”的位置时遇到了一些问题

从中,我找到了(默认)文件

删除Ubuntu上的Index.php CodeIgniter URL

  • 初步说明。 我正在以root权限运行本教程中的所有步骤,因此请确保您以root身份登录:

    sudo su
    
  • 在apache上启用mod_重写模块。 首先按如下方式启用模块:

    a2enmod rewrite
    
    将所有出现的“AllowOverRide None”更改为“AllowOverRide all”。基本上,以下文件中的所有“无”到“全部”:

     gedit **/etc/apache2/sites-available/default**
    
    重新启动Apache:

    /etc/init.d/apache2 restart
    
  • 打开CodeIgniter上的config.php文件(application\config)。 删除
    $config['index_page']=“index.php”上的index.php

  • 在CodeIgniter根目录上创建file.htaccess。 用以下代码填充文件:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
    
  • 没有
    $config['index_page'] = "";
    
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
    
    <Directory --your root dir-- />
        AllowOverride All #was "none", but this was all commented out
        Order Deny,Allow
        Deny from all
    </Directory>
    
    DirectoryIndex index.php
    RewriteEngine on
    RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
    
    RewriteEngine on
    RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
    RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
    
    DirectoryIndex index.php
    RewriteEngine on
    RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]