diff --git a/src/ApacheModRewriteGenerator.php b/src/ApacheModRewriteGenerator.php index 45f3f1b..dc2ebe4 100644 --- a/src/ApacheModRewriteGenerator.php +++ b/src/ApacheModRewriteGenerator.php @@ -3,6 +3,7 @@ namespace donatj\RewriteGenerator; use donatj\RewriteGenerator\Exceptions\AmbiguousRelativeHostException; +use donatj\RewriteGenerator\Exceptions\MismatchedSchemeException; use donatj\RewriteGenerator\Exceptions\UnhandledUrlException; use InvalidArgumentException; @@ -29,7 +30,8 @@ public function generateRewrite( string $from, string $to, int $type ) : string ); } - $toScheme = $parsedTo['scheme'] ?? ''; + $fromScheme = $parsedFrom['scheme'] ?? ''; + $toScheme = $parsedTo['scheme'] ?? ''; $fromHost = $parsedFrom['host'] ?? ''; $toHost = $parsedTo['host'] ?? ''; @@ -47,6 +49,11 @@ public function generateRewrite( string $from, string $to, int $type ) : string 'Unclear relative host. When the "FROM" URI specifies a HOST the "TO" MUST specify a HOST as well.' ); } + + if( $fromScheme && $toScheme && $toHost && $fromScheme !== $toScheme && $fromHost === $toHost ) { + throw new MismatchedSchemeException('Scheme specified on "FROM" of "' . $fromScheme . '" and "TO" of "' . $toScheme . '" do not match.'); + } + if( $toHost && $fromHost !== $toHost ) { $output .= 'RewriteCond %{HTTP_HOST} ^' . preg_quote($fromHost, ' ') . '$'; $output .= "\n"; diff --git a/src/Exceptions/MismatchedSchemeException.php b/src/Exceptions/MismatchedSchemeException.php new file mode 100644 index 0000000..f337e38 --- /dev/null +++ b/src/Exceptions/MismatchedSchemeException.php @@ -0,0 +1,7 @@ + https://baz.qux/quux +RewriteCond %{HTTP_HOST} ^foo\.bar$ +RewriteRule ^baz$ https://baz.qux/quux? [L,R=301] + +TAG + , + <<<'TAG' +# Rewrite --- http://foo.bar/baz => https://baz.qux/quux +RewriteCond %{HTTP_HOST} ^foo\.bar$ +RewriteRule ^baz$ https://baz.qux/quux?&%{QUERY_STRING} + +TAG + , + ]; } /** @@ -204,6 +225,22 @@ public function failureProvider() : Generator { , 3, ]; + // Ensure an error is thrown if the HOST is the same but the SCHEME varies + yield [ + <<<'TAG' +http://foo.bar/baz https://foo.bar/baz +TAG + , + <<<'TAG' +# WARNING: Input contained 1 error(s) + +# 301 --- http://foo.bar/baz => https://foo.bar/baz +# ERROR: Scheme specified on "FROM" of "http" and "TO" of "https" do not match.: http://foo.bar/baz https://foo.bar/baz + +TAG + , 1, + ]; + yield [ <<<'TAG' foo.html#funk bar.html