Skip to content

Commit

Permalink
Option to match via full URL or path only
Browse files Browse the repository at this point in the history
  • Loading branch information
smirnov-tk committed Apr 30, 2022
1 parent 6413cbb commit 204feb6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ Use regular expressions to match the full URI being requested. The service catch
### `redirect` (string, required)
The fully qualified redirect URI. The bundle will set the protocol (http/https) based on the incoming original request so it ports from dev to prod easily.

### `full_url` (bool, optional)
Defines whether to match pattern on full URL (with scheme, domain, path and query) or only path and query (___default: false__)

### `status` (int, optional)
Set the status code (__default: 301__) for the redirection. Tip: use 302 while debugging to avoid 301 permanent redirects from being cached in the browser.

Expand Down Expand Up @@ -79,7 +82,9 @@ autologic_redirect:
- { pattern: '/.*old-route/', redirect: 'domain.com/new-route' }
# match subdomains and more complex patterns and use parameters
- { pattern: '/au\..+?\.[^\/]+.*blog\/old-australian-blog-post-on-any-domain-of-subdomain/',
redirect: 'au.%base_domain%/news/new-australian-news-article' }
redirect: 'au.%base_domain%/news/new-australian-news-article',
full_url: true
}
```

## Logging
Expand Down
6 changes: 5 additions & 1 deletion Service/RedirectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ public function __construct(ContainerInterface $container)
public function redirect(Request $request)
{
$url = $request->getUri();
$path = $request->getRequestUri();

foreach ($this->rules as $redirect) {
$redirect = (new Redirect())->fromConfigRule($redirect)->withRequest($request);
preg_match($redirect->getPattern(), $url, $matches);

preg_match($redirect->getPattern(), $redirect->isFullURLMatch() ? $url : $path, $matches);

if (!empty($matches)) {
return new RedirectResponse(
$redirect->getProtocol().$redirect->getRedirectURI().$redirect->getPath(),
Expand Down
22 changes: 18 additions & 4 deletions ValueObject/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class Redirect
*/
private $absolute = null;

/**
* @var null|bool
*/
private $fullURLMatch = false;

public function __construct()
{
$this->request = new Request();
Expand Down Expand Up @@ -178,14 +183,23 @@ public function fromConfigRule($rule)
$redirect = clone $this;
$redirect->pattern = $rule['pattern'];
$redirect->redirect = $rule['redirect'];
$redirect->URIForwarding = isset($rule['forwarding']) ? $rule['forwarding'] : false;
$redirect->statusCode = isset($rule['status']) ? $rule['status'] : Response::HTTP_MOVED_PERMANENTLY;
$redirect->protocol = isset($rule['protocol']) ? $rule['protocol'] : null;
$redirect->absolute = isset($rule['absolute']) ? $rule['absolute'] : null;
$redirect->fullURLMatch = $rule['full_url'] ?? false;
$redirect->URIForwarding = $rule['forwarding'] ?? false;
$redirect->statusCode = $rule['status'] ?? Response::HTTP_MOVED_PERMANENTLY;
$redirect->protocol = $rule['protocol'] ?? null;
$redirect->absolute = $rule['absolute'] ?? null;

return $redirect;
}

/**
* @return bool
*/
public function isFullURLMatch()
{
return $this->fullURLMatch;
}

/**
* @return string
*/
Expand Down

0 comments on commit 204feb6

Please sign in to comment.