【发布时间】:2019-07-02 11:41:41
【问题描述】:
这是一个项目。我需要将某些传入的 URL 请求重定向到托管在同一网络服务器上的文件。我不能使用 .htaccess 也不能依赖任何插件。
我设法用一个插件做到了这一点,但我很难提取必要的代码来编写我自己的插件来完成我需要做的硬编码。
- WordPress 多站点
所有软件都是最新的
“重定向”WordPress 插件(成功)
- 编写自定义 WP 函数来执行此操作(半成功)
我在 Pantheon 文档中找到了一些代码:
$blog_id = get_current_blog_id();
// You can easily put a list of many 301 url redirects in this format
// Trailing slashes matters here so /old-url1 is different from /old-url1/
$redirect_targets = array(
'/test/test.xml' => '/files/' . $blog_id . '/test.xml',
'/regex/wildcard.xml(.*)' => '/files/' . $blog_id . '/regex.xml',
);
if ( (isset($redirect_targets[ $_SERVER['REQUEST_URI'] ] ) ) && (php_sapi_name() != "cli") ) {
echo 'https://'. $_SERVER['HTTP_HOST'] . $redirect_targets[ $_SERVER['REQUEST_URI'] ];
header('HTTP/1.0 301 Moved Permanently');
header('Location: https://'. $_SERVER['HTTP_HOST'] . $redirect_targets[ $_SERVER['REQUEST_URI'] ]);
if (extension_loaded('newrelic')) {
newrelic_name_transaction("redirect");
}
exit();
}
https://example.com/test/test.xml成功
重定向到
/files/5/text.xml
但是,一些传入的请求包含查询字符串,例如
https://example.com/regex/wildcard.xml?somequerystring
显然,来自
的重定向https://example.com/regex/wildcard.xml
到
/files/5/regex.xml
工作正常。
但是,一旦涉及查询字符串,重定向就不起作用。鉴于我需要使用 PHP 执行此操作,如何实现从 /regex/* 或 /regex/wildcard.xml* 到 /files/5/regex.xml 的通配符重定向?
任何帮助将不胜感激。
谢谢, 丹尼尔
【问题讨论】:
-
谢谢!不幸的是,我认为它并不真正适用于我的问题。本质上,我只需要找出如何在 php: '/regex/wildcard.xml(.*)' => '/files/' 中使用通配符。 $blog_id 。 '/regex.xml',
-
如果有任何帮助,这就是我使用插件设法做到的方式:howarddc.com/understanding-redirection-regular-expressions“创建通配符重定向”
-
检查任何 URL 重定向插件
-
我写信给他们,到目前为止没有回复,只是从代码中我无法弄清楚。
标签: regex wordpress redirect url-redirection http-status-code-301