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

Car*_*ers 20 .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)
RewriteRule ^(.*)$ /index.php/$1 [L]
Run Code Online (Sandbox Code Playgroud)

但我不明白这是如何工作的.我应该把.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"
Run Code Online (Sandbox Code Playgroud)

并将其添加到.htaccess文件中:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Run Code Online (Sandbox Code Playgroud)

并将其保存在我的站点根目录中(在本例中/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>
Run Code Online (Sandbox Code Playgroud)

但它仍然无法正常工作.如果我导航到http://localhost/cms/index.php/hello,它会打印"Hello there",但http://localhost/cms/hello会出现404错误.

想法?

更新:græt成功!

我不得不深入挖掘我的apache配置文件,最终找到所有目录的定义位置(我在此日期之前从未弄乱过服务器配置文件),并发现那令人烦恼的AllowOverride None事情正在挫败我的伟大计划.

现在它完美无缺.这里的所有答案都是可行和有效的.

Ran*_*ell 18

在你的system/application/config/config.php,改变

$config['index_page'] = "index.php";
Run Code Online (Sandbox Code Playgroud)

$config['index_page'] = "";
Run Code Online (Sandbox Code Playgroud)

在你的.htaccess,添加这个:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|stylesheets|scripts|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Run Code Online (Sandbox Code Playgroud)

将.htaccess文件添加到system文件夹所在的同一目录中,因此在CodeIgniter根目录中,您应该具有

  • 系统/
  • 用户指南/
  • 的index.php
  • 的.htaccess
  • LICENSE.TXT

此外,由于您的服务器上运行了多个站点,因此您可能希望拥有VirtualHost.将此添加到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>
Run Code Online (Sandbox Code Playgroud)

您现在可以使用访问该站点http://localhost:11000/并访问该控制器http://localhost:11000/hello.


sys*_*bug 17

在这里尝试了答案,但这对我不起作用.所以我尝试了以下内容,它确实......

在根目录中创建.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]  
Run Code Online (Sandbox Code Playgroud)

完成了!尝试它,它肯定会工作.希望能帮助到你


The*_*emz 5

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

本指南中我找到了(默认)文件.

在Ubuntu上删除Index.php CodeIgniter URL

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

    sudo su
    
    Run Code Online (Sandbox Code Playgroud)
  2. 在apache上启用mod_rewrite模块.首先启用模块如下:

    a2enmod rewrite
    
    Run Code Online (Sandbox Code Playgroud)

    将所有出现的"AllowOverRide None"更改为"AllowOverRide All".基本上所有"无"到"全部"在以下文件中:

     gedit **/etc/apache2/sites-available/default**
    
    Run Code Online (Sandbox Code Playgroud)

    重启Apache:

    /etc/init.d/apache2 restart
    
    Run Code Online (Sandbox Code Playgroud)
  3. 在CodeIgniter(application\config)上打开文件config.php.删除index.php$config['index_page'] = "index.php";

    $config['index_page'] = "";
    
    Run Code Online (Sandbox Code Playgroud)
  4. 在CodeIgniter根目录上创建文件.htaccess.使用以下代码填充文件:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
    
    Run Code Online (Sandbox Code Playgroud)

现在CodeIgniter URL上的Index.php消失了