I've this NGINX rewrite URL:

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

I want to convert the rule to PHP because my host does not let me change the server_name in NGINX to this: *.example.com

I've tried something like this: (This script is located in index.php at my domain and I've turned DNS-wildcard on)

<?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**

As you can see, it's parsing the url with parse_url and convert it back to string from the parsed url.

Unfortunately this script is not working I'll get the content of the HTML page. Somebody has an answer?

Thanks in advance.

(Function unparse_url comes from here)

有帮助吗?

解决方案

Something like this?

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归因
不隶属于 StackOverflow
scroll top