【问题标题】:How do I write a .htaccess file to make CodeIgniters URL routing work?如何编写 .htaccess 文件以使 CodeIgniters URL 路由工作?
【发布时间】: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


    【解决方案1】:

    将它放在您网站的文档根目录中。

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

    如果您有多个站点从同一目录运行,则可以使用 RewriteCond 将特定站点排除或包含到重写规则中。

    您可以以同样的方式排除某些目录(例如 /articles)。

    【讨论】:

      【解决方案2】:

      在你的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 根目录中,您应该有

      • 系统/
      • user_guide/
      • 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>
      

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

      【讨论】:

      • 但是 .htaccess 在 /var/www/cms/ 中,我将基本 URL 配置为 http://localhost/cms。无论如何,这两个 URL 都不起作用
      • 这使它拒绝连接,如果我也将端口更改为 80 也是如此。我一定是在做某事大错特错,我不认为这通常很难做到。
      • Ubuntu linux。但是你知道吗,不是所有东西都在一个“httpd.conf”文件中,而是散布在一堆文件中。我想,因为根本没有任何工作,所以我深入研究了所有配置包括并找到了目录的定义位置,并在那里更改了 AllowOverride。它还不起作用,但现在 .htaccess 已实际打开,我将再次尝试所有操作。
      • 我已阅读您的回答。是的,将AllowOverride 设置为None 并不容易。每次安装几乎只能修改一次,因此经常被遗忘。
      • +1 兰德尔你的权利。我遇到了同样的问题,解决了它。谢谢。
      【解决方案3】:

      这个特定的 .htaccess 文件看起来需要进入您网站的根目录(即当前正在执行的网站)。

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

      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      

      mod_rewrite 非常强大,但如果它没有按照您认为的那样做,它可能会很痛苦。我也不知道有什么方法可以调试它的指令。

      至于您可以使用 .htaccess 文件做什么,Apache docs for htaccess files 始终是一个不错的起点。最常见的用途是针对每个目录的身份验证。 .htaccess 文件中支持大多数服务器配置指令(前提是服务器的 AllowOverride 这么说),但有些语法不同。

      【讨论】:

        【解决方案4】:
        <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>
        

        【讨论】:

        • 我也将它粘贴到我的 httpd.conf 文件中,但没有成功。
        【解决方案5】:

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

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

        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]  
        

        完成!试试看,它肯定会奏效。 希望对你有帮助

        【讨论】:

          【解决方案6】:

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

          this guide 我找到了(默认)文件。

          删除 Ubuntu 上的 Index.php CodeIgniter URL

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

            sudo su
            
          2. 在 apache 上启用 mod_rewrite 模块。 首先启用模块如下:

            a2enmod rewrite
            

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

             gedit **/etc/apache2/sites-available/default**
            

            重启 Apache:

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

            $config['index_page'] = "";
            
          4. 在 CodeIgniter 根目录上创建文件 .htaccess。 使用此代码填充文件:

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

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

          【讨论】:

            【解决方案7】:

            我在使用 CodeIgnitor 链接图像、js、css 时遇到了同样的问题。 现在一切正常感谢你们!我已按照以下步骤操作。将所有其他 cmets 合并到我的场景中才能工作。希望这对某人有所帮助。

            1. 更改 httpd.conf

              <Directory --your root dir-- />
                  AllowOverride All #was "none", but this was all commented out
                  Order Deny,Allow
                  Deny from all
              </Directory>
              
            2. 在 htdocs\yoursite(site root) 目录中创建了一个空的 .htaccess 文件并粘贴以下内容。

              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]
              

            【讨论】:

              【解决方案8】:
              RewriteEngine on
              RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
              RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
              

              根文件夹中的.htaccess 中的简单!对我有用。

              【讨论】:

                【解决方案9】:

                试试这个,效果很好

                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]  
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2013-01-23
                  • 1970-01-01
                  • 2018-03-15
                  • 2017-12-04
                  • 2015-09-22
                  相关资源
                  最近更新 更多