gpt4 book ai didi

apache - 尝试为丢失的文件强制使用状态代码 200 的 RewriteRule 重定向

转载 作者:行者123 更新时间:2023-12-01 15:32:39 25 4
gpt4 key购买 nike

不要马上生气 :P,我正在探索各种选择,我有理由对此进行调查。

我(可以)在文件夹 /scripts/competitions/*.js 中有脚本文件。

现在,脚本文件将由服务器端脚本写入此文件夹中。所以在某些时候,特定的比赛脚本文件可能不可用。因此,尝试加载该脚本文件的网站会得到一个不错的 404。

现在,我可以就此打住,除非您打开 Dev Tools,否则您不会注意到 404,对吗?但是,我宁愿让我的服务器始终为该特定文件夹返回 200,无论文件是否存在。

对于此网址:/scripts/competitions/A9ACCC8F-B78A-4E0D-8023-51D8F56B5C55.js

我当前的重写代码可以很好地加载文件,但不会将状态代码更改为 201(只是一个测试)。

ReWriteRule ^/scripts/competitions/([^.]*).js$ /scripts/competitions/$1.js [R=201]

所以我认为正则表达式没有正确捕获。

当我把它改成

ReWriteRule ^scripts/competitions/([^.]*).js$ /scripts/competitions/$1.js [R=201]

它确实重定向到 201,但在 Created 标题下给我一条错误消息

Created

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at webmaster@localhost to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.

Apache/2.4.7 (Ubuntu) Server at tracker.test.local Port 80

如果脚本文件确实存在(它确实存在用于该错误消息测试),那么它会捕获、重定向但不会加载脚本文件。

所以我所追求的是正常的 200 响应加上 JS 正文(当它存在时)和 200 加上空正文(如果可能的话最好)或找不到文件的错误消息。

从我的日志中转储第一个重写规则(以 ^/ 开头):

[Tue Nov 24 20:51:40.594882 2015] [rewrite:trace3] [pid 3593:tid 139723926361856] mod_rewrite.c(468): [client 192.168.1.6:55261] 192.168.1.6 - - [tracker.test.local/sid#7f14130f5618][rid#7f14130540a0/initial] [perdir /var/www/test/tracker/root/] strip per-dir prefix: /var/www/test/tracker/root/scripts/competitions/A9ACCC8F-B78A-4E0D-8023-51D8F56B5C55.js -> scripts/competitions/A9ACCC8F-B78A-4E0D-8023-51D8F56B5C55.js
[Tue Nov 24 20:51:40.595077 2015] [rewrite:trace3] [pid 3593:tid 139723926361856] mod_rewrite.c(468): [client 192.168.1.6:55261] 192.168.1.6 - - [tracker.test.local/sid#7f14130f5618][rid#7f14130540a0/initial] [perdir /var/www/test/tracker/root/] applying pattern '^/scripts/competitions/([^.]*).js$' to uri 'scripts/competitions/A9ACCC8F-B78A-4E0D-8023-51D8F56B5C55.js'
[Tue Nov 24 20:51:40.595313 2015] [rewrite:trace1] [pid 3593:tid 139723926361856] mod_rewrite.c(468): [client 192.168.1.6:55261] 192.168.1.6 - - [tracker.test.local/sid#7f14130f5618][rid#7f14130540a0/initial] [perdir /var/www/test/tracker/root/] pass through /var/www/test/tracker/root/scripts/competitions/A9ACCC8F-B78A-4E0D-8023-51D8F56B5C55.js

希望有人能把我推向正确的方向。

*更新当文件确实存在时从日志中转储,它显示 201,但也显示遇到内部错误的消息

[Tue Nov 24 21:27:31.134452 2015] [rewrite:trace3] [pid 3593:tid 139723842434816] mod_rewrite.c(468): [client 192.168.1.6:55684] 192.168.1.6 - - [tracker.test.local/sid#7f14130f5618][rid#7f14130440a0/initial] [perdir /var/www/test/tracker/root/] strip per-dir prefix: /var/www/test/tracker/root/scripts/competitions/A9ACCC8F-B78A-4E0D-8023-51D8F56B5C55.js -> scripts/competitions/A9ACCC8F-B78A-4E0D-8023-51D8F56B5C55.js
[Tue Nov 24 21:27:31.134479 2015] [rewrite:trace3] [pid 3593:tid 139723842434816] mod_rewrite.c(468): [client 192.168.1.6:55684] 192.168.1.6 - - [tracker.test.local/sid#7f14130f5618][rid#7f14130440a0/initial] [perdir /var/www/test/tracker/root/] applying pattern '^scripts/competitions/([^.]*)\\.js$' to uri 'scripts/competitions/A9ACCC8F-B78A-4E0D-8023-51D8F56B5C55.js'
[Tue Nov 24 21:27:31.134495 2015] [rewrite:trace2] [pid 3593:tid 139723842434816] mod_rewrite.c(468): [client 192.168.1.6:55684] 192.168.1.6 - - [tracker.test.local/sid#7f14130f5618][rid#7f14130440a0/initial] [perdir /var/www/test/tracker/root/] forcing responsecode 201 for /var/www/test/tracker/root/scripts/competitions/A9ACCC8F-B78A-4E0D-8023-51D8F56B5C55.js

最佳答案

当文件不存在时,使用服务器端脚本作为回退。此外,前导目录(包括斜杠)已从 .htaccess 规则中删除。

RewriteCond %{REQUEST_URI} !-f
RewriteRule ^(scripts/competitions/)[^./]*\.js$ $1not_exist.php [L]

它不一定是 PHP,它可以只是一个静态 js 文件,因此它以正确的 MIME 类型提供。你只需要一些东西来返回默认内容。将默认值放在与比赛文件相同的目录中,并在规则中引用它。内容可以是:

<?php
header('Content-Type: application/javascript');
echo '// ', basename($_SERVER['REQUEST_URI']), ' does not exist yet.';

关于apache - 尝试为丢失的文件强制使用状态代码 200 的 RewriteRule 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33890423/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com