繁体   English   中英

将NGINX重写规则转换为PHP

[英]Convert NGINX rewrite rule to PHP

我有这个NGINX重写URL:

if ($http_host !~ "([a-zA-Z0-9.-]+)\.example\.com(.+)") {
    rewrite  ^ http://example.com/browse.php?u=http://$1$2? permanent;
}

我想将规则转换为PHP,因为主机不允许我将NGINX中的server_name更改为:* .example.com

我已经尝试过类似的操作:(此脚本位于我域中的index.php中,并且我已打开DNS通配符

<?php
function unparse_url($parsed_url) { 
    $scheme   = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : ''; 
    $host     = isset($parsed_url['host']) ? $parsed_url['host'] : ''; 
    $port     = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : ''; 
    $path     = isset($parsed_url['path']) ? $parsed_url['path'] : ''; 
    $query    = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : ''; 
    $fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : ''; 
}
if(preg_match('/^([a-zA-Z0-9.-]+)\.example\.com\/(.+)$/', $_SERVER["SERVER_NAME"])){
    unparse_url(parse_url($_SERVER["SERVER_NAME"]));
    header('Location: $schemeexample.com/browse.php?u=$scheme$host$port$path$query$fragment');
}
else {
?>
<html>
**HTML CONTENT HERE**

如您所见,它使用parse_url解析URL,并将其从解析的URL转换回字符串。

不幸的是,该脚本无法正常工作,我将获得HTML页面的内容。 有人有答案吗?

提前致谢。

函数unparse_url来自此处

像这样吗

if(preg_match('#([\w\.-]+)\.example\.com(.+)#', $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'], $match)) {
    header('Location: http://example.com/browse.php?u=http://'.$match[1].$match[2]);
    die;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM