【问题标题】:Why is this .htaccess file mod_rewrite not working?为什么这个 .htaccess 文件 mod_rewrite 不起作用?
【发布时间】:2015-10-15 14:09:34
【问题描述】:

我正在使用 MAMP 和 CodeIgniter。 我网站的根目录是:/Users/Roy/Websites/CodeIgniter-3.0.2 在网站文件夹中,我还有一些其他项目,但我认为这并不重要。这是 apache httpd.conf 文件:http://pastebin.com/Am0ew0C0

在我的 .htaccess 文件中,我使用以下内容:

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

<IfModule authz_core_module>
   Require all denied
</IfModule>
<IfModule !authz_core_module>
   Deny from all
</IfModule>

.htaccess 文件位于:/Users/Roy/Websites/CodeIgniter-3.0.2/.htaccess 我不知道为什么 mod_rewrite 不起作用,它应该消除在 url 中使用 index.php 因为我现在必须使用的 URL 是:http://localhost:8888/CodeIgniter-3.0.2/index.php/about

我希望它变成:http://localhost:8888/CodeIgniter-3.0.2/about 使用 PHP 版本 5.6.10。 apache没有读取.htaccess文件,问题出在这里,如何解决?

【问题讨论】:

  • 不使用 index.php 时会收到什么消息?

标签: php apache .htaccess codeigniter mod-rewrite


【解决方案1】:

你可以用这个

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /Project_Name
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
  ErrorDocument 404 /index.php
</IfModule>

【讨论】:

  • 感谢您输入 russel,但由于某种原因,整个 .htaccess 文件没有被读取。
【解决方案2】:

问题解决了,安装了 MAMP 的 httpd.conf 没有必要弄乱了。只需获取 CodeIgniter 并将其放在 MAMP 内的 htdocs 文件夹中。之后在 CodeIgniter 文件夹(不是里面的应用程序文件夹)中创建一个 .htaccess 文件并使用以下代码:

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteBase /ci/
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

现在您有了干净的 URL,不再需要使用 /index.php/about,而只需使用 /about。

【讨论】:

    【解决方案3】:

    这可能是因为您在 httpd.conf 文件中将文档根目录设置为 /Users/Roy/Websites/。如果您计划拥有多个网站,我个人会使用virtualhosts,这样每个网站都可以拥有自己的.conf,并且您可以专门为每个网站设置文档根目录。

    无论如何,现在您的CodeIgniter-3.0.2Websites 的子文件夹,它是您的文档根目录。所以很可能你在重写基础上遇到了问题。

    如果您的文件和 .htaccess 在 codeigniter 文件夹中,您的 .htaccess 将需要如下所示。

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

    【讨论】:

    • 感谢您的建议,但是当我将所有内容粘贴到 .htaccess 文件中并重新启动 MAMP 以确保我仍然得到相同的东西时。使用 localhost:8888/CodeIgniter-3.0.2/index.php/about 时,我看到了我的网页,但是当我使用 localhost:8888/CodeIgniter-3.0.2/about 时,我从 CodeIgniter 获得了默认的未找到页面,这是一个屏幕截图:imgur.com/65H4mxu
    • 您是否将 CI 配置更新为 $config['index_page'] = '';
    • 尝试改写RewriteRule ^(.*)$ /CodeIgniter-3.0.2/index.php/$1 [L]
    • 重启 MAMP 后仍然找不到相同的页面,似乎它甚至没有读取这个 .htaccess 文件,我应该检查错误日志吗?
    • 检查错误日志,同时在你的 .htaccess 文件顶部添加随机文本。它不会给你一个 500 错误它没有读取它。
    【解决方案4】:

    看起来像文件权限问题。检查 .htaccess 文件的属性并更改权限。

    尝试使用此代码。

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

    更多请尝试http://w3code.in/2015/09/how-to-remove-index-php-file-from-codeigniter-url

    【讨论】:

      【解决方案5】:

      .htaccess 可能非常令人沮丧。对真正有效的东西多加努力

      1.您的 .htaccess 应该如下所示

      <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteBase /booking/
      RewriteCond %{REQUEST_URI} ^system.*
      RewriteRule ^(.*)$ /index.php?/$1 [L]
      RewriteCond %{REQUEST_URI} ^application.*
      RewriteRule ^(.*)$ /index.php?/$1 [L]
      
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)$ index.php?/$1 [L]
      
      </IfModule>
      

      2.将第3行替换为你项目的根目录名,在我自己的例子中是'localhost/booking/'

      3.将.htaccess 文件从应用程序文件夹复制到根目录。这意味着您现在必须在整个项目中使用 .htaccess 文件的实例。

      4.在您的 config.php 中,在我自己的情况下,基本 url 应该是这样的

      $config['base_url'] = 'http://localhost/booking/'
      

      5.

      $config['index_page'] = '';
      

      6.

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

      7。在 httpd.conf 文件中打开 Mod-rewrite,方法是删除此行前面的 #

      LoadModule rewrite_module modules/mod_rewrite.so
      

      希望能解决你的问题

      【讨论】:

        猜你喜欢
        • 2013-11-14
        • 2012-12-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-24
        • 1970-01-01
        • 2015-02-26
        • 2017-06-12
        相关资源
        最近更新 更多