【问题标题】:.htaccess not working for CodeIgniter project on Mac OS.htaccess 不适用于 Mac OS 上的 CodeIgniter 项目
【发布时间】:2014-09-23 14:14:03
【问题描述】:

不久前,我在大部分项目中都使用了 CodeIgniter。我正在回到事物的摇摆中并遇到一些颠簸。我现在在 Mac OSX Mavericks 上开发并使用 MAMP 作为我的 Apache 服务器。一切似乎都运行良好,但我始终无法摆脱 URL 中的 index.php。我已将 .htaccess 文件添加到项目的根目录中。这是文件:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /ez-recruits/

#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]

#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
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> 

我不太了解如何制作这些文件,所以通常我只是从 CodeIgniters 网站获取它们。我在 URL 中没有 index.php 时收到 404 错误,如果我手动输入它,页面将正常加载。此外,我在 Firefox 中进行开发,并且在 URL 中没有 index.php 的情况下收到错误消息“Firefox 无法理解该地址”。我确实在 Apache 服务器上激活了 mod_rewrite。我已在索引页面的配置文件中删除了 index.php,并将 base_url 设置为

$config['base_url'] = 'localhost:8888/ez-recruits';

我做了一些研究,发现更改httpd.conf文件对其他人有帮助,因此我将其中的一部分更改为:

<Directory />
    Options FollowSymLinks
    AllowOverride All
    Order deny,allow
    Deny from all
</Directory>

但它什么也没改变。有谁知道我可能做错了什么,或者这只是一个 Mac 问题?此外,使用所有这些配置,我是否会难以与在 Windows 机器上开发的其他人一起开发?

提前致谢。

【问题讨论】:

    标签: php apache .htaccess codeigniter mod-rewrite


    【解决方案1】:

    确保在config/autoload.php 中加载了以下帮助程序

    $autoload['helper'] = array('url','file');
    

    【讨论】:

      【解决方案2】:

      你需要在config.php文件中使用base_urlindex_page

      $config['base_url']     = 'http://yourlocalurl/';
      $config['index_page'] = 'index.php';
      

      【讨论】:

      • 您实际上不需要提供任何一个:)
      【解决方案3】:

      为您的 .htaccess 试试这个

      <IfModule mod_rewrite.c>
          RewriteEngine On
      
          #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]
      
          #When your application folder isn't in the system folder
          #This snippet prevents user access to the application folder
          #Submitted by: Fabdrol
          #Rename 'application' to your applications folder name.
          RewriteCond %{REQUEST_URI} ^application.*
          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 %{ENV:REDIRECT_STATUS} ^$
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteCond %{REQUEST_FILENAME} !-d
          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>
      

      此外,您可以将$config['base_url'] 留空,如果您在多个系统上工作,这会让您的工作更轻松。

      希望这会有所帮助!

      【讨论】:

      • 我现在就把它放进去。但是我应该将项目的基本文件夹放在那个 .htaccess 中的什么位置呢?通常我知道在顶部我必须添加到项目文件夹中。我想我将不得不将 base_url 留空,谢谢!
      • 我一直发现不包含 RewriteBase 会更容易,除非我特别需要!
      • 很高兴我能帮上忙! :)
      猜你喜欢
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 2017-08-02
      • 1970-01-01
      • 2015-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多