【问题标题】:htaccess rewrite - not rewriting to index.phphtaccess 重写 - 不重写到 index.php
【发布时间】:2013-06-25 19:04:23
【问题描述】:

我在三台不同的计算机上安装了三台网络服务器。我正在尝试安装一个 php 应用程序,它将所有 url 请求重写为 index.php(前端控制器模式)。以下 .htaccess 适用于三台 Web 服务器中的两台:

RewriteEngine On
# Redirect everything to the Front Controller
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^(css|images|files)/ index.php [NC,L]

所以,当我访问 http://example.com/hithere 时,会调用 index.php,然后我可以解析最初请求的 url (hithere) 以确定将其发送到哪个控制器。

但是,由于某些未知原因,上述 .htaccess 文件在第三个网络服务器上不起作用。相反,当我访问 http://example.com/hithere 时,我的浏览器中出现 404 not found 错误,提示如下:

The requested URL /full/path/to/application/index.php was not found on this server.

因此,基于上述错误,我可以看出它实际上是在尝试重定向到 index.php。但是浏览器在下面/full/path/... 中列出的文件实际上在我的网络服务器上——所以我不知道为什么它找不到它。有人可以帮助我并告诉我我做错了什么吗?

编辑:我不知道 httpd.conf 中是否有冲突,但这里是我在 httpd.conf 中针对此应用程序所在目录的设置

Alias /myapp /full/path/to/my/app
<Directory /full/path/to/my/app>
Order allow,deny
AllowOverride All
Allow from all
</Directory>

编辑 2: 我注意到 Apache 日志中有一些特殊之处。看起来 Apache 正在将我试图将流量重定向到的 index.php 附加到 DocumentRoot:

File does not exist: /document/root/full/path/to/my/app/index.php

什么时候应该去:

/full/path/to/my/app/index.php

所以我基本上需要告诉 htaccess 忽略文档根目录。我通过在我的 .htaccess 文件中指定这个来尝试这个:

RewriteBase /full/path/to/my/app/

但它仍在为 index.php 搜索文档根目录:/ 有没有办法重置 DocumentRoot 或告诉 htaccess 在重写 index.php 时忽略它?即使我在 RewriteRule 中使用绝对路径,它仍然会将其附加到文档根目录。

RewriteEngine On
# Redirect everything to the Front Controller
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^(css|images|files)/ /full/path/to/my/app/index.php [NC,L]

这对我来说没有意义...有人可以帮忙吗?

谢谢

【问题讨论】:

  • 第三个 Web 服务器是否启用了 mod_rewrite?
  • 第三个 Web 服务器是否将 AllowOverride 设置为您的 .htaccess 所在目录的足够高的级别?
  • @faffaffaff 是的,httpd.conf 文件包含 AllowOverride All。我已经使用此应用程序的相关 httpd.conf 设置编辑了问题,也许它在那里?感谢您的帮助
  • 检查您的 apache 日志。
  • 你能显示ls -lZ /full/path/to/application/的输出吗?

标签: php apache .htaccess mod-rewrite


【解决方案1】:

因此,基于上述错误,我可以看出它实际上是在尝试 重定向到 index.php。但是浏览器在 /full/path/... 下面实际上是在我的网络服务器上 - 所以我不知道 为什么找不到。

假设它是一个类似 *nix 的系统,您可能需要检查文件权限并确保网络服务器具有对该文件的读取权限。

【讨论】:

  • 我检查了权限,它们是正确的。不过好主意
  • @jerry 服务器上的操作系统是什么?如果是 Linux 服务器,哪个发行版?
  • 操作系统是 CentOs。此外,该问题似乎与 httpd.conf 中的 DocumentRoot 设置有关。请参阅我的问题的第二次编辑。
【解决方案2】:

还需要检查httpd.conf中的DocumentRoot参数 在文件中查找类似的内容并确保它是正确的。这也是 apache 的来源。

DocumentRoot "/var/www/html/path/to/whatever"

另外,如果 SELinux 已打开,而您真的不需要它,我也会禁用它,因为它也会导致奇怪的问题。

【讨论】:

    【解决方案3】:

    首先使用$ sudo a2enmod rewrite在apache中启用重写模块,然后通过$ sudo systemctl restart apache2重新启动Web服务器 将以下代码放入您的虚拟主机中。

    <Directory /var/www/html/your_app_folder>
        Order allow,deny
        AllowOverride All
        Allow from all
    </Directory>
    

    然后在您的.htaccess 文件中添加以下代码

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

    【讨论】:

      猜你喜欢
      • 2012-05-19
      • 1970-01-01
      • 2012-01-06
      • 2013-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-04
      相关资源
      最近更新 更多