【发布时间】:2022-01-31 04:11:19
【问题描述】:
我有这个静态 html 页面的配置,其中重定向也是从平面文件完成的:
old-location.html
<!-- {new-location.html} -->
<!DOCTYPE html>
<html>
<head>
<title>Redirecting to new location</title>
<link rel="canonical" href="new-location.html" />
<meta name="robots" content="noindex, follow">
<noscript><meta http-equiv="refresh" content="0;URL='new-location.html'" /></noscript>
<script>window.location = "new-location.html"</script>
</head>
<body>
<a href="new-location.html">[old-location.html moved here]</a>
</body>
html 应该只作为一个优雅的后备链,而第一行的注释是一个配置钩子,用于正确的http 重定向,因为没有运行时环境,需要由网络服务器完成。
目前我在 Openresty 中执行此操作,使用 Lua 的匹配模式,找到新位置,然后将其设置为 301 重定向。
header_filter_by_lua_block {
local address = ngx.var.document_root .. ngx.var.document_uri
local file = io.open(address, "rb")
local content = file:read(100)
file:close()
local location = string.match(content, "{(%g+)}")
ngx.header['location'] = location
ngx.status = 301
}
-- ideally I should intercept the response body and spare the extra file read, but it seems that even with Lua this is not possible
但是,并不是每个人都接受切换到新的网络服务器。所以我想知道是否有一种方法可以使用现成的 Nginx,或者更好的是,使用大多数/所有网络服务器都支持的通用方法。
由于 服务器端包含(有没有办法从文件中检索 ssi 变量并将其设置为标头?)和 sub_module(替换除了可能需要一些正则表达式的部分?)两者都已经解析了整个身体,我认为可能有一种方法,但我不太知道从哪里开始寻找。
【问题讨论】:
-
如果你使用的是JS,你应该能够解析你需要的html标签。你想解析什么,我可以给你一个正则表达式?
-
JS 没有用,因为标头已经发送(除非我使用 nginx 的 njs,这不会比现在好多少)。上面的匹配模式——“{(%g+)}”——已经足够好了,唯一的问题是它需要一个非常罕见的网络服务器。
-
我希望能帮助你,但我的专长是正则表达式。我在 html/xml/sgml 解析方面有丰富的经验,但看起来你不需要它。
标签: apache nginx webserver litespeed server-side-includes