为了账号安全,请及时绑定邮箱和手机立即绑定

使用 htaccess 删除 server.php 出现错误 500

使用 htaccess 删除 server.php 出现错误 500

PHP
慕婉清6462132 2021-12-24 15:39:55
我有一个可以通过两种方式访问的网站:http://localhost/public/bloghttp://localhost/server.php/blog我使用下面的代码从 url 中删除“public”及其工作。<IfModule mod_rewrite.c>    RewriteEngine On    RewriteRule ^(.*)$ public/$1 [L]</IfModule>所以我尝试从上面的代码更改public/为server.php/。但它不起作用。<IfModule mod_rewrite.c>    RewriteEngine On    RewriteRule ^(.*)$ server.php/$1 [L]</IfModule>服务器返回错误代码 500。服务器日志日志报告:[Wed Sep 04 01:06:53.801523 2019] [core:error] [pid 546] [client 182.253.16.222:36164] AH00124:由于可能的配置错误,请求超出了 10 个内部重定向的限制。如有必要,请使用“LimitInternalRecursion”来增加限制。使用“LogLevel debug”获取回溯。所以我对第二个代码的期望是地址“ http://localhost/server.php/blog ”可以在没有 server.php 的情况下访问(http://localhost/blog)。
查看完整描述

2 回答

?
肥皂起泡泡

TA贡献1829条经验 获得超6个赞

您展示的两个示例都具有有效的语法并且应该可以工作,但是您似乎仍然存在问题,这表明两种不同的重定向模式以某种方式相互干扰,或者您有一个循环重定向。


验证您的配置是否正常工作

我刚刚建立了一个简约的 apache 项目来试验重写规则:https : //github.com/rpagliuca/apache-rewrite-minimalist-environment


在那个项目中,我测试了一个与您描述的非常相似的配置:


# Apache virtual host config file: vhost.conf

<VirtualHost *:80>

    DocumentRoot /var/www/html

    RewriteEngine On

    LogLevel alert rewrite:trace6

    RewriteRule ^(.*)$ /server.php$1 [L]

</VirtualHost>

另外,我在项目中添加了 2 个不同的 PHP 文件:


# File 1: server.php

Handled by server.php<br/>

URI: <?= $_SERVER['REQUEST_URI'] ?>

第二个文件类似:


# File 1: server.php

Handled by index.php<br/>

URI: <?= $_SERVER['REQUEST_URI'] ?>

于是我开始与项目docker-compose up --build,发现使用容器的IPdocker ps和docker inspect,并测试了以下网址:http://192.168.16.2/blog。我在浏览器上得到以下输出:


Handled by server.php

URI: /blog

这个测试表明这个 RewriteRule 在简单的情况下是有效的(没有循环重定向——对于循环重定向的场景,请阅读@Anonymous answer)。


如果我注释掉该行RewriteEngine On,我会在 URL 上收到 404 错误http://192.168.16.2/blog,这支持重定向规则确实有效的结论。此外,index.php只有在RewriteEngine On被注释掉时才能访问。否则,当启用重定向引擎时,我会得到以下输出:


Handled by server.php

URI: /index.php

终极解决方案

你可能已经注意到了线LogLevel alert rewrite:trace6中vhost.conf上面的文件。


该行可以调试 Apache 2.4 或更高版本的重写规则。有了它,您可以更轻松、更快速地修复未来的重定向规则错误。


每当我使用docker-compose up --build,运行我的服务器并点击/blogURI 时,我都会得到以下输出:


app_1_d7cddb74e8bd | [Wed Sep 04 12:27:02.121396 2019] [rewrite:trace2] [pid 17] mod_rewrite.c(483): [client 192.168.16.1:49620] 192.168.16.1 - - [192.168.16.2/sid#7fa770113d98][rid#7fa7737780a0/initial] init rewrite engine with requested uri /blog

app_1_d7cddb74e8bd | [Wed Sep 04 12:27:02.121497 2019] [rewrite:trace3] [pid 17] mod_rewrite.c(483): [client 192.168.16.1:49620] 192.168.16.1 - - [192.168.16.2/sid#7fa770113d98][rid#7fa7737780a0/initial] applying pattern '^(.*)$' to uri '/blog'

app_1_d7cddb74e8bd | [Wed Sep 04 12:27:02.121534 2019] [rewrite:trace2] [pid 17] mod_rewrite.c(483): [client 192.168.16.1:49620] 192.168.16.1 - - [192.168.16.2/sid#7fa770113d98][rid#7fa7737780a0/initial] rewrite '/blog' -> '/server.php/blog'

app_1_d7cddb74e8bd | 192.168.16.2:80 192.168.16.1 - - [04/Sep/2019:12:27:02 +0000] "GET /blog HTTP/1.1" 200 267 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36"

app_1_d7cddb74e8bd | [Wed Sep 04 12:27:02.121550 2019] [rewrite:trace2] [pid 17] mod_rewrite.c(483): [client 192.168.16.1:49620] 192.168.16.1 - - [192.168.16.2/sid#7fa770113d98][rid#7fa7737780a0/initial] local path result: /server.php/blog

app_1_d7cddb74e8bd | [Wed Sep 04 12:27:02.121618 2019] [rewrite:trace2] [pid 17] mod_rewrite.c(483): [client 192.168.16.1:49620] 192.168.16.1 - - [192.168.16.2/sid#7fa770113d98][rid#7fa7737780a0/initial] prefixed with document_root to /var/www/html/server.php/blog

app_1_d7cddb74e8bd | [Wed Sep 04 12:27:02.121630 2019] [rewrite:trace1] [pid 17] mod_rewrite.c(483): [client 192.168.16.1:49620] 192.168.16.1 - - [192.168.16.2/sid#7fa770113d98][rid#7fa7737780a0/initial] go-ahead with /var/www/html/server.php/blog [OK]

app_1_d7cddb74e8bd | [Wed Sep 04 12:27:02.122014 2019] [rewrite:trace2] [pid 17] mod_rewrite.c(483): [client 192.168.16.1:49620] 192.168.16.1 - - [192.168.16.2/sid#7fa770113d98][rid#7fa7736c30a0/subreq] init rewrite engine with requested uri /blog

app_1_d7cddb74e8bd | [Wed Sep 04 12:27:02.122032 2019] [rewrite:trace3] [pid 17] mod_rewrite.c(483): [client 192.168.16.1:49620] 192.168.16.1 - - [192.168.16.2/sid#7fa770113d98][rid#7fa7736c30a0/subreq] applying pattern '^(.*)$' to uri '/blog'

app_1_d7cddb74e8bd | [Wed Sep 04 12:27:02.122048 2019] [rewrite:trace2] [pid 17] mod_rewrite.c(483): [client 192.168.16.1:49620] 192.168.16.1 - - [192.168.16.2/sid#7fa770113d98][rid#7fa7736c30a0/subreq] rewrite '/blog' -> '/server.php/blog'

app_1_d7cddb74e8bd | [Wed Sep 04 12:27:02.122060 2019] [rewrite:trace2] [pid 17] mod_rewrite.c(483): [client 192.168.16.1:49620] 192.168.16.1 - - [192.168.16.2/sid#7fa770113d98][rid#7fa7736c30a0/subreq] local path result: /server.php/blog

app_1_d7cddb74e8bd | [Wed Sep 04 12:27:02.122079 2019] [rewrite:trace2] [pid 17] mod_rewrite.c(483): [client 192.168.16.1:49620] 192.168.16.1 - - [192.168.16.2/sid#7fa770113d98][rid#7fa7736c30a0/subreq] prefixed with document_root to /var/www/html/server.php/blog

app_1_d7cddb74e8bd | [Wed Sep 04 12:27:02.122091 2019] [rewrite:trace1] [pid 17] mod_rewrite.c(483): [client 192.168.16.1:49620] 192.168.16.1 - - [192.168.16.2/sid#7fa770113d98][rid#7fa7736c30a0/subreq] go-ahead with /var/www/html/server.php/blog [OK]

您可以按照这些日志,轻松找出循环重定向或任何其他错误行为发生的位置。


修复后记得关闭 mod_rewrite 调试,尤其是在生产环境下。


查看完整回答
反对 回复 2021-12-24
?
九州编程

TA贡献1785条经验 获得超4个赞

您正在重定向每个 URL,但您应该只在server.php丢失时重定向。


<IfModule mod_rewrite.c>

    RewriteEngine On

    RewriteCond %{REQUEST_URI} !^server\.php($|/)

    RewriteRule ^(.*)$ server.php/$1 [L]

</IfModule>


查看完整回答
反对 回复 2021-12-24
  • 2 回答
  • 0 关注
  • 155 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号