【问题标题】:Codeigniter doesn't work without index.php没有 index.php,Codeigniter 不起作用
【发布时间】:2013-02-26 01:45:36
【问题描述】:

我正在使用具有别名作品的 MAMP。 Codeigniter 位于localhost/works/project/web

如果没有 index.php,控制器将无法工作 (localhost/works/project/web/index.php/auth/register)

$config['base_url'] = 'localhost/works/project/web/'; //with http://

$config['index_page'] = '';

$config['uri_protocol'] = 'AUTO'; 

(我为 uri_protocol 尝试了所有这些)

我在 /User/me/works/project/web/ 创建和编辑 .htaccess 文件 我厌倦了所有关于这个问题的 .htaccess 文件。

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /Users/me/works/project/web

#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#‘system’ can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]

#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn’t true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#This last condition enables access to the images and css folders, and the robots.txt file
#Submitted by Michael Radlmaier (mradlmaier)
RewriteCond $1 !^(index\.php|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
# If we don’t have mod_rewrite installed, all 404’s
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin

ErrorDocument 404 /index.php
</IfModule>

创建错误,即: [error] [client ::1] File does not exist: /Users/me/works/project/web/auth

当我使用 RewriteBase 时也是一样的/

mod_rewrite 在 phpInfo 中处于活动状态。

我找不到解决办法。

【问题讨论】:

标签: .htaccess codeigniter codeigniter-2 httpd.conf codeigniter-url


【解决方案1】:

您的RewriteBase 错误。它不应该是您服务器的相对文件路径,它应该是您的重写规则等的相对 URL 路径。 More info here.

如果您曾经使用过 HTML 的 &lt;base&gt; 标签,那么原理几乎相同。如果您的主 localhost URL 是 http://localhost,并且您的项目位于子文件夹 http://localhost/my-project 中,那么您将使用 RewriteBase /my-project/

如果您没有使用 RewriteBase,并且您的项目位于子文件夹中,则必须将该子文件夹添加到您的每个 URL 和重写中,如下所示:

RewriteRule ^(.*)$ /my-project/index.php/$1 [L]

深思:

  • 尽可能简单开始您的 .htaccess 文件。您立即投入的越多,出错的可能性就越大,调试起来就越困难。特别是如果您是 .htaccess 文件的菜鸟。不要盲目地复制和粘贴——弄清楚实际做了什么。

  • 系统文件夹访问部分现在是多余的——系统文件夹有自己的 .htaccess 来限制请求,就像应用程序文件夹一样。

【讨论】:

    【解决方案2】:

    1.在 application/config.php 文件中进行以下更改

    $config['base_url'] = 'http://'.$_SERVER['SERVER_NAME'].'/Your Ci folder_name';
    $config['index_page'] = '';
    $config['uri_protocol'] = 'AUTO';
    

    2.在.htaccess文件中使用这个:

    RewriteEngine on
    RewriteCond $1 !^(index\.php|resources|robots\.txt)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L,QSA]
    

    并使用以下命令启用重写模式

     a2enmod rewrite
    

    并编辑文件 /etc/apache2/sites-enabled/000-default

    change the AllowOverride None to AllowOverride All.
    

    最后重启你的服务器

    【讨论】:

      【解决方案3】:

      你肯定没有打开 mod_rewrite 或者你没有用 MAMP 安装它

      打开你的 httpd 配置文件并更改

      # AllowOverride controls what directives may be placed in .htaccess files.
          # It can be "All", "None", or any combination of the keywords:
          #   Options FileInfo AuthConfig Limit
          #
          AllowOverride None
      

      AllowOverride All
      

      取消注释这一行:

      #LoadModule rewrite_module libexec/apache2/mod_rewrite.so 
      

      同样在此处对用户进行一些更改:/etc/apache2/users/username.conf

      LoadModule php5_module        libexec/apache2/libphp5.so
      
      DocumentRoot "/Users/username/where/ever/you/like"
      
      <Directory "/Users/username/where/ever/you/like">
          Options Indexes MultiViews +FollowSymLinks
          AllowOverride All
          Order allow,deny
          Allow from all
      </Directory>
      

      在此处获取更多信息http://adam.merrifield.ca/2010/08/09/apache-mod_rewrite-php-and-os-x-10-6-snow-leopard/


      重启 MAMP


      要了解 mod_rewrite 是否开启,请按照 Stackoverflow 中的答案进行操作:

      How to check if mod_rewrite is enabled in php?


      只是为了确保您的 .htaccess 不是问题,您可以试试这个。已经测试过了:

      <IfModule mod_rewrite.c>
      
          RewriteEngine on
          RewriteBase /Users/me/works/project/web/
      
          # If the start of the URL doesn't match one of these...
          RewriteCond $1 !^(index\.php|robots\.txt|assets|cache|themes|uploads|css|images|js)
      
          # Route through index.php
          RewriteRule ^(.*)$ index.php/$1 [L]
      
      </IfModule>
      

      上面授予了对某些文件夹的访问权限,并且重写库有一个尾部斜杠。


      确保您有任何其他嵌套的 .htaccess 覆盖这个。

      【讨论】:

      • 您将 .htaccess 文件放在哪里?这些目录中的哪个(/Users/me/works/project/web/)是您的默认 localhost 目录(我猜是 web),哪个当前保存着您的 .htaccess?我猜也是网络。您的 .htacess 也应该与 index.php 文件位于同一目录中。你能告诉我更多关于你的目录结构的细节吗?
      • "web" 目录有这些:应用程序系统用户指南 index.php .htacces(我正在使用的那个)在应用程序目录中还有一个 .htaccess 其中包括:Deny from all 我试图改变它也是,但没有工作。
      • 不,不要改变那个。 web 也是您的默认 MAMP 文件夹还是子目录。如果项目是默认的 MAMP 文件夹,您可以更改 rewritebase /web/.如果web是,rewritebase / 然后重启apache。
      • 请看我编辑的回复。确保您已打开 mod_rewrite。它可能未安装或在您当前的 MAMP 安装中打开。 Mod_rewrite 是 apache,您可能必须进行我上面指出的更改。这应该会为您解决问题。
      • 我没有使用 .htacces 来解决问题,而是将它们写入 httpd.conf。谢谢。
      【解决方案4】:

      我没有使用 .htacces 来解决问题,而是将它们写入 httpd.conf。 我检查了 AllowOverride is All 但我不确定为什么 .htaccess 不起作用。因为我在这个问题上浪费了太多时间,所以我将使用这种方式。 感谢您的回答。

      【讨论】:

        【解决方案5】:

        在您的虚拟主机上使用 AllowOverride All

        <Directory "/path/to/your/site">
            ....
            AllowOverride All
        </Directory>
        

        【讨论】:

          【解决方案6】:

          打开你的 httpd 配置文件 LoadModule rewrite_module modules/mod_rewrite.so 然后 AllowOverride All

                    <Directory "C:/xampp/htdocs"> //example for xampp
                         Options Indexes FollowSymLinks Includes ExecCGI
                         AllowOverride All
                         Order allow,deny
                         Allow from all
                     </Directory>
          

          【讨论】:

            【解决方案7】:

            我已将 codeigniter 安装在如下格式的 url 中:http://11.12.34.45/~project/

            我尝试了不同的建议,最后这个以下网址提供了解决方案:

            https://ellislab.com/expressionengine/user-guide/urls/remove_index.php.html

            【讨论】:

              【解决方案8】:

              你需要修改Apache的两个文件, httpd.conf httpd-vhosts.conf

              在 httpd.conf 中更改它

              <Files ".ht*">
              Require all denied
              </Files>
              

              到,

              <Files ".ht*">
              Require all granted
              </Files>
              

              并在 httpd-vhosts.conf 中添加以下代码,

              <VirtualHost *:8000>
              ServerName localhost
              DocumentRoot c:/wamp64/www
              <Directory  "c:/wamp64/www/">
                  Options +Indexes +Includes +FollowSymLinks +MultiViews
                  AllowOverride All
                  Allow from all
              </Directory>
              </VirtualHost>
              

              【讨论】:

                猜你喜欢
                • 2012-01-17
                • 1970-01-01
                • 2016-06-19
                • 2014-08-12
                • 2013-12-29
                • 2019-01-14
                • 1970-01-01
                • 2014-11-21
                • 1970-01-01
                相关资源
                最近更新 更多