【问题标题】:.htaccess rewriterule not working correct on mac.htaccess rewriterule 在 Mac 上无法正常工作
【发布时间】:2011-12-18 00:33:14
【问题描述】:

我的 mac book pro leopard 上有一个网站项目,我使用的是内置的 apache2 和 php。我已经在 httpd.conf 和 user.conf 中进行了配置以使用 htaccess。所做的更改如下:

LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so

<Directory >
    AllowOverride All
</Directory>

问题是当我想打开一个网站时 localhost/~username/site/site/index.php/welcome,index.php做一些操作,找到正确的控制器和正确的页面。

但是当我尝试进入像 site/welcome 这样的网站时,apache 给了我以下错误:

找不到

在此服务器上未找到请求的 URL Users/username/Sites/site/index.php/welcome。

问题是 apache 得到这个就像它确实是一个文件,并且错误语句以文件系统的方式给出它,但是这个请求必须被提取到 index.php。

我的 .htaccess 文件看起来像这样

<ifModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [NC,QSA,L]
</ifModule>

让我感到困扰的是,此配置在 Linux 和 Windows 上都适用,但在 Mac 上却不适用。我觉得注定要失败:)

【问题讨论】:

  • 能否确认重写模块已正确启用?
  • 是的,我可以在 phpinfo() 的加载模块部分看到它们;
  • 您会考虑更改您接受的答案吗?你接受的那个实际上是不正确的。 @tong 的回答是正确的。

标签: php macos apache .htaccess mod-rewrite


【解决方案1】:

我遇到了同样的问题,但对我来说,问题是 mod_rewrite 未启用。

我不得不在 /etc/apache2/httpd.conf 文件中取消注释这两行:

LoadModule rewrite_module libexec/apache2/mod_rewrite.so
LoadModule php7_module        libexec/apache2/libphp7.so

之后就不需要使用RewriteBase等了

【讨论】:

    【解决方案2】:

    如果我将 .htaccess 放入 /Library/WebServer/Documents 并打开“localhost/”;对其进行测试,这可以按预期工作。它只是在“~/Sites”中不起作用。

    【讨论】:

      【解决方案3】:

      我只是遇到了同样的问题,但上面的解决方案对我不起作用,

      从一开始就删除斜线就可以了(我猜是因为我的浏览器已经在正确的用户下运行了)。

      所以 RewriteRule ^Register /register.php

      变成了RewriteRule ^Register register.php

      这让我发疯了!顺便说一句,我在 mac os 10.8 上运行 xampp (apache 2)。

      【讨论】:

        【解决方案4】:

        在 Mac OSX Mountain Lion 上,确保 AllowOverride 在目录上设置为 All

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

        和你的文档根路径的根目录:

        <Directory "/Library/WebServer/Documents">
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            Allow from all
        </Directory>
        

        还要确保FollowSymLinks 包含在Options 中。

        如果您的“无 index.php 的漂亮 URL”不在 DocumentRoot 上,则在您的 index.php 所在的 .htaccess 文件中添加以下 RewriteBase 目录,如下例所示:

        RewriteBase /~username/site/site/
        

        如果您想将.htaccess 文件名更改为.aclhtaccess.txt 之类的其他名称,请在您的httpd.conf 中取消注释以下行

        # Various default settings
        Include /private/etc/apache2/extra/httpd-default.conf
        

        并将AccessFileName 指令中的.htaccess 更改为extra/httpd-default.conf 文件中您喜欢的任何内容。例如,如果您想将 .htaccess 文件更改为 htaccess.txt,请将其更改为:

        AccessFileName htaccess.txt
        

        保存它们,在 OSX Mountain Lion 上重新启动您的 apache Web 服务器,希望您一切顺利。 希望这会有所帮助。

        【讨论】:

          【解决方案5】:

          我也遇到了同样的问题! 'DocumentRoot' 在 Lion 中对我不起作用。

          但这有效:

          RewriteEngine On
          
          RewriteBase /~username/YOURPath
          
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteRule ^(.*)$ index.php/$1 [NC,QSA,L]
          

          【讨论】:

          • 太棒了!我花了大约三个小时试图弄清楚这一点。谢谢!
          • 我遇到了同样的问题,但它节省了我的时间,所以 +1
          • 这应该是公认的答案,因为它是正确的 - 应该始终设置 RewriteBase。修改 DocumentRoot 是一种 hack(在配置良好的服务器上通过 .htaccess 是不可能的)。
          • 我有同样的问题,但 RewriteBase 没有工作 AH00526:/usr/local/etc/apache2/2.4/sites-enabled/example.com.conf 第 8 行的语法错误:RewriteBase:仅在每个目录的配置文件中有效
          【解决方案6】:

          您需要将以下行添加到您的 .htaccess 文件中:

          DocumentRoot "/~username/site/"
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-02-19
            • 2016-09-17
            • 1970-01-01
            • 2015-04-17
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多