将NGINX重写规则转换为PHP

将NGINX重写规则转换为PHP,php,regex,nginx,preg-match,parse-url,Php,Regex,Nginx,Preg Match,Parse Url,我有一个NGINX重写URL: if ($http_host !~ "([a-zA-Z0-9.-]+)\.example\.com(.+)") { rewrite ^ http://example.com/browse.php?u=http://$1$2? permanent; } 我想将规则转换为PHP,因为我的主机不允许我将NGINX中的服务器名称更改为:*.example.com 我尝试过这样的方法:(这个脚本位于我的域的index.php中,我已经打开了DNS通配符) **

我有一个NGINX重写URL:

if ($http_host !~ "([a-zA-Z0-9.-]+)\.example\.com(.+)") {
    rewrite  ^ http://example.com/browse.php?u=http://$1$2? permanent;
}
我想将规则转换为PHP,因为我的主机不允许我将NGINX中的服务器名称更改为:*.example.com

我尝试过这样的方法:(这个脚本位于我的域的index.php中,我已经打开了DNS通配符)


**这里的HTML内容**
如您所见,它使用parse_url解析url,并将其从解析后的url转换回字符串

不幸的是,这个脚本不起作用,我将获取HTML页面的内容。有人有答案吗

提前谢谢

像这样的事

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;
}
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;
}