
Untitled
By: a guest on
Jul 20th, 2012 | syntax:
None | size: 1.14 KB | hits: 9 | expires: Never
Convert NGINX rewrite rule to PHP
if ($http_host !~ "([a-zA-Z0-9.-]+).example.com(.+)") {
rewrite ^ http://example.com/browse.php?u=http://$1$2? permanent;
}
<?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**
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;
}