Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/ApacheModRewriteGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace donatj\RewriteGenerator;

use donatj\RewriteGenerator\Exceptions\AmbiguousRelativeHostException;
use donatj\RewriteGenerator\Exceptions\MismatchedSchemeException;
use donatj\RewriteGenerator\Exceptions\UnhandledUrlException;
use InvalidArgumentException;

Expand All @@ -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'] ?? '';
Expand All @@ -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";
Expand Down
7 changes: 7 additions & 0 deletions src/Exceptions/MismatchedSchemeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace donatj\RewriteGenerator\Exceptions;

class MismatchedSchemeException extends GenerationException {

}
37 changes: 37 additions & 0 deletions tests/tests/ApacheIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,27 @@ public function exampleProvider() : Generator {
,
];

// Assure Redirects across Schemes do not error.
yield [
<<<'TAG'
http://foo.bar/baz https://baz.qux/quux
TAG
,
<<<'TAG'
# 301 --- http://foo.bar/baz => 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
,
];
}

/**
Expand Down Expand Up @@ -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
Expand Down