【问题标题】:Apache 2.4 mod_rewrite returns 404 for every RewriteRuleApache 2.4 mod_rewrite 为每个 RewriteRule 返回 404
【发布时间】:2019-05-16 19:39:55
【问题描述】:

我在 FreebBSD 11.2-RELEASE-p5 服务器上设置 Apache 2.4.35,我在其上启用了 mod_rewrite 模块,但没有任何重写规则起作用,我花了几天时间寻找答案但无法做到工作。

这是我在服务器上运行 httpd -M 得到的结果:

Loaded Modules:
 core_module (static)
 so_module (static)
 http_module (static)
 mpm_prefork_module (shared)
 authn_file_module (shared)
 authn_core_module (shared)
 authz_host_module (shared)
 authz_groupfile_module (shared)
 authz_user_module (shared)
 authz_core_module (shared)
 auth_basic_module (shared)
 reqtimeout_module (shared)
 filter_module (shared)
 mime_module (shared)
 log_config_module (shared)
 env_module (shared)
 headers_module (shared)
 setenvif_module (shared)
 version_module (shared)
 unixd_module (shared)
 status_module (shared)
 autoindex_module (shared)
 dir_module (shared)
 alias_module (shared)
 rewrite_module (shared)
 php7_module (shared)

这是我的虚拟主机的 Apache 配置:

<VirtualHost *:80>
        ServerAdmin mail@domain.com
        DocumentRoot /usr/local/www/htdocs/myweb
        ServerName myweb.mywebsite.com

        <Location />
                SetEnvIfNoCase Request_URI  \
                        \.(?:gif|jpe?g|png)$ no-gzip dont-vary
                SetEnvIfNoCase Request_URI  \
                        \.(?:exe|t?gz|zip|gz2|sit|rar)$ no-gzip dont-vary
        </Location>
</VirtualHost>

###################################################################################

<Directory "/usr/local/www/htdocs/myweb">
        Options All MultiViews
        AllowOverride All
        Require all granted
        LogLevel debug rewrite:trace8
        DirectoryIndex index.html index.php
</Directory>

这是位于 /usr/local/www/htdocs/myweb 文件夹的 .htaccess 文件的第一行

RewriteEngine on
RewriteBase /

#Login
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^users/login(/?)$ /users/login.php [L]

我重新启动了 Apache 无数次,错误日志中没有记录任何内容,我尝试的每个规则总是返回 404 错误,我不知道还能做什么,我不确定是否需要启用另一个模块或我还需要做什么。

提前非常感谢!

更新

根据@KasperAgg 的要求,这是附加信息

请求:

POST http://myweb.mywebsite.com/users/login

错误日志:

[Sun Dec 16 14:29:40.349713 2018] [core:info] [pid 12094] [client 10.0.0.71:20255] AH00128: File does not exist: /usr/local/www/htdocs/myweb/users/login

访问日志:

[16/Dec/2018:14:29:40 -0800] "POST /users/login HTTP/1.1" 404 209

/usr/local/www/htdocs/myweb/users/ 文件夹:

-rw-r--r--  1 www  wheel   909 Dec 12 21:59 delete.php
-rw-r--r--  1 www  wheel   928 Dec 12 21:59 get.php
-rw-r--r--  1 www  wheel   892 Dec 12 21:59 getById.php
-rw-r--r--  1 www  wheel  1111 Dec 12 21:59 login.php
-rw-r--r--  1 www  wheel   742 Dec 12 21:59 logout.php
-rw-r--r--  1 www  wheel  2262 Dec 12 21:59 save.php

对我来说奇怪的是服务器的行为就像 mod_rewrite 没有打开一样,并且正在寻找 users/login 文件夹而不是将 URL 重写为 users/login.php 脚本

【问题讨论】:

  • (...)/myweb 的内容是什么?
  • 在 .htaccess 文件中还有包含 PHP 脚本的文件夹,这是一个 RESTful API,需要 mod_rewrite 将友好的 url 重写为处理请求所需的 PHP 脚本
  • 例如哪个 url 失败了,它是否可以在没有重写的情况下工作? Afaik 目录侦探应该是虚拟主机的一部分......
  • 所有的重写都失败了,没有一个起作用,我列出的那个失败了,是你在问什么还是我误解了你的问题?
  • 我看到一个重写(有条件),但如果实际的“目标”不存在(index.php),你可能会得到一个 404。你能展示一个你的 URL 的例子吗'正在尝试,404日志(apache错误/访问日志)和目录的实际内容?

标签: apache mod-rewrite url-rewriting http-status-code-404


【解决方案1】:

我能够解决问题,它变成了有指令

RewriteEngine Off

在我的 .htaccess 文件的末尾破坏了重写功能,奇怪的是,这个配置是从另一台服务器复制的,基本上是这个服务器的克隆,因此我从未遇到任何错误,但是,现在它正在工作,这可能会帮助另一个人

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-04
    • 1970-01-01
    • 2015-08-24
    • 1970-01-01
    • 2016-05-09
    • 2016-01-19
    • 2016-04-06
    相关资源
    最近更新 更多