.htaccess RewriteEngine 在 xampp 中导致 Unexpected 403 Forbidden http 响应代码

Posted

技术标签:

【中文标题】.htaccess RewriteEngine 在 xampp 中导致 Unexpected 403 Forbidden http 响应代码【英文标题】:.htaccess RewriteEngine causing Unexpected 403 Forbidden http response code in xampp 【发布时间】:2021-10-10 00:50:52 【问题描述】:

我正在使用 xampp 测试一些 php 代码。

在主项目目录中,我有以下 .htaccess 文件:

RewriteEngine on

#Index
RewriteCond %REQUEST_FILENAME ^/$  
RewriteRule request_handler.php [L]

#php files
RewriteCond %REQUEST_FILENAME !-d
RewriteCond %REQUEST_FILENAME !-f [OR]
RewriteCond %REQUEST_FILENAME ^.*(\.php)$
RewriteRule . request_handler.php [L]

#Blocked files. PHP blocked files should be specified in request_handler.php
<Files "setup.json">
    Order Allow,Deny
    Deny from all
</Files>
<Files "testimonials.json">
    Order Allow,Deny
    Deny from all
</Files>

#CACHE CONTROLL FOR STATIC RESOURSES
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
    Header set Cache-Control "max-age=2592000, public"
</FilesMatch>

无论如何,我在所有请求中都遇到了意外的 403 禁止。但是,如果我删除 .htaccess 文件的第一行,我将不再出现这种意外行为。

有谁知道是什么导致了这个错误?

更新 我创建了一个新的 .htaccess 文件,它只包含一行:RewriteEngine On,但我仍然收到错误消息。 (如果我删除该行,我将停止收到错误)。该行的唯一存在会导致错误。

【问题讨论】:

【参考方案1】:

事实证明,我所要做的就是在 RewriteEngine On 指令之前添加行 Options FollowSymLinks

这是 Apache 不得不说的:

要在此上下文中启用重写引擎,您需要设置“RewriteEngine On”并且必须启用“Options FollowSymLinks”。 [...] 出于安全原因,需要此限制。

Apache documentation

【讨论】:

以上是关于.htaccess RewriteEngine 在 xampp 中导致 Unexpected 403 Forbidden http 响应代码的主要内容,如果未能解决你的问题,请参考以下文章

text 在RewriteEngine On之后,将此代码段添加到Joomla网站的.htaccess

.htaccess RewriteEngine 在 xampp 中导致 Unexpected 403 Forbidden http 响应代码

apache_conf 强大的.htaccess片段,需要在最顶层使用RewriteEngine On

APACHE RewriteEngine用途

RewriteEngine on 给出错误 403

<IfModule mod_rewrite.c> 中的 RewriteEngine 每次都打开?