diff --git a/src/Application/Routers/Route.php b/src/Application/Routers/Route.php index 7d423275c..8a2589e51 100644 --- a/src/Application/Routers/Route.php +++ b/src/Application/Routers/Route.php @@ -161,12 +161,13 @@ public function match(Nette\Http\IRequest $httpRequest) if ($this->type === self::HOST) { $host = $url->getHost(); $path = '//' . $host . $url->getPath(); - $host = ip2long($host) ? [$host] : array_reverse(explode('.', $host)); + $parts = ip2long($host) ? [$host] : array_reverse(explode('.', $host)); $re = strtr($re, [ '/%basePath%/' => preg_quote($url->getBasePath(), '#'), - '%tld%' => preg_quote($host[0], '#'), - '%domain%' => preg_quote(isset($host[1]) ? "$host[1].$host[0]" : $host[0], '#'), - '%sld%' => preg_quote(isset($host[1]) ? $host[1] : '', '#'), + '%tld%' => preg_quote($parts[0], '#'), + '%domain%' => preg_quote(isset($parts[1]) ? "$parts[1].$parts[0]" : $parts[0], '#'), + '%sld%' => preg_quote(isset($parts[1]) ? $parts[1] : '', '#'), + '%host%' => preg_quote($host, '#'), ]); } elseif ($this->type === self::RELATIVE) { @@ -400,6 +401,7 @@ public function constructUrl(Application\Request $appRequest, Nette\Http\Url $re '%tld%' => $parts[0], '%domain%' => isset($parts[1]) ? "$parts[1].$parts[0]" : $parts[0], '%sld%' => isset($parts[1]) ? $parts[1] : '', + '%host%' => $host, ]); $url = ($this->scheme ?: $refUrl->getScheme()) . ':' . $url; } else { diff --git a/tests/Routers/Route.variables.phpt b/tests/Routers/Route.variables.phpt index c39cde5b8..d534e8120 100644 --- a/tests/Routers/Route.variables.phpt +++ b/tests/Routers/Route.variables.phpt @@ -55,6 +55,13 @@ testRouteIn(new Route('//%sld%.%tld%/', 'Default:default'), '/abc', 'Defau ], '/abc?test=testvalue'); +testRouteIn(new Route('//%host%/', 'Default:default'), '/abc', 'Default', [ + 'path' => 'abc', + 'action' => 'default', + 'test' => 'testvalue', +], '/abc?test=testvalue'); + + // alternative testRouteIn(new Route('//example.%tld%/', 'Default:default'), '/abc', 'Default', [ 'path' => 'abc', @@ -97,3 +104,10 @@ Assert::same('http://localhost/', $route->constructUrl($route->match($httpReques $route = new Route('//%tld%/', 'Default:default'); Assert::same('http://localhost/', $route->constructUrl($route->match($httpRequest), $url)); + + +// host +$url = new Nette\Http\UrlScript('http://www.example.com/'); +$httpRequest = new Nette\Http\Request($url); +$route = new Route('//%host%/', 'Default:default'); +Assert::same('http://www.example.com/', $route->constructUrl($route->match($httpRequest), $url));