mirror of
https://github.com/shlinkio/shlink.git
synced 2024-12-23 07:33:58 -06:00
Defined new options for new redirect configuration
This commit is contained in:
parent
8b9663aea0
commit
24c3a3e84c
@ -6,7 +6,7 @@ return [
|
|||||||
|
|
||||||
'not_found_redirects' => [
|
'not_found_redirects' => [
|
||||||
'invalid_short_url' => null, // Formerly url_shortener.not_found_short_url.redirect_to
|
'invalid_short_url' => null, // Formerly url_shortener.not_found_short_url.redirect_to
|
||||||
'404' => null,
|
'regular_404' => null,
|
||||||
'base_url' => null,
|
'base_url' => null,
|
||||||
],
|
],
|
||||||
|
|
||||||
|
@ -102,7 +102,7 @@ This is the complete list of supported env vars:
|
|||||||
* `DELETE_SHORT_URL_THRESHOLD`: The amount of visits on short URLs which will not allow them to be deleted. Defaults to `15`.
|
* `DELETE_SHORT_URL_THRESHOLD`: The amount of visits on short URLs which will not allow them to be deleted. Defaults to `15`.
|
||||||
* `VALIDATE_URLS`: Boolean which tells if shlink should validate a status 20x (after following redirects) is returned when trying to shorten a URL. Defaults to `true`.
|
* `VALIDATE_URLS`: Boolean which tells if shlink should validate a status 20x (after following redirects) is returned when trying to shorten a URL. Defaults to `true`.
|
||||||
* `INVALID_SHORT_URL_REDIRECT_TO`: If a URL is provided here, when a user tries to access an invalid short URL, he/she will be redirected to this value. If this env var is not provided, the user will see a generic `404 - not found` page.
|
* `INVALID_SHORT_URL_REDIRECT_TO`: If a URL is provided here, when a user tries to access an invalid short URL, he/she will be redirected to this value. If this env var is not provided, the user will see a generic `404 - not found` page.
|
||||||
* `404_REDIRECT_TO`: If a URL is provided here, when a user tries to access a URL not matching any one supported by the router, he/she will be redirected to this value. If this env var is not provided, the user will see a generic `404 - not found` page.
|
* `REGULAR_404_REDIRECT_TO`: If a URL is provided here, when a user tries to access a URL not matching any one supported by the router, he/she will be redirected to this value. If this env var is not provided, the user will see a generic `404 - not found` page.
|
||||||
* `BASE_URL_REDIRECT_TO`: If a URL is provided here, when a user tries to access Shlink's base URL, he/she will be redirected to this value. If this env var is not provided, the user will see a generic `404 - not found` page.
|
* `BASE_URL_REDIRECT_TO`: If a URL is provided here, when a user tries to access Shlink's base URL, he/she will be redirected to this value. If this env var is not provided, the user will see a generic `404 - not found` page.
|
||||||
* `BASE_PATH`: The base path from which you plan to serve shlink, in case you don't want to serve it from the root of the domain. Defaults to `''`.
|
* `BASE_PATH`: The base path from which you plan to serve shlink, in case you don't want to serve it from the root of the domain. Defaults to `''`.
|
||||||
* `REDIS_SERVERS`: A comma-separated list of redis servers where Shlink locks are stored (locks are used to prevent some operations to be run more than once in parallel).
|
* `REDIS_SERVERS`: A comma-separated list of redis servers where Shlink locks are stored (locks are used to prevent some operations to be run more than once in parallel).
|
||||||
@ -155,7 +155,7 @@ The whole configuration should have this format, but it can be split into multip
|
|||||||
"short_domain_host": "doma.in",
|
"short_domain_host": "doma.in",
|
||||||
"validate_url": false,
|
"validate_url": false,
|
||||||
"invalid_short_url_redirect_to": "https://my-landing-page.com",
|
"invalid_short_url_redirect_to": "https://my-landing-page.com",
|
||||||
"404_redirect_to": "https://my-landing-page.com",
|
"regular_404_redirect_to": "https://my-landing-page.com",
|
||||||
"base_url_redirect_to": "https://my-landing-page.com",
|
"base_url_redirect_to": "https://my-landing-page.com",
|
||||||
"redis_servers": [
|
"redis_servers": [
|
||||||
"tcp://172.20.0.1:6379",
|
"tcp://172.20.0.1:6379",
|
||||||
@ -174,7 +174,7 @@ The whole configuration should have this format, but it can be split into multip
|
|||||||
```
|
```
|
||||||
|
|
||||||
> This is internally parsed to how shlink expects the config. If you are using a version previous to 1.17.0, this parser is not present and you need to provide a config structure like the one [documented previously](https://github.com/shlinkio/shlink-docker-image/tree/v1.16.3#provide-config-via-volumes).
|
> This is internally parsed to how shlink expects the config. If you are using a version previous to 1.17.0, this parser is not present and you need to provide a config structure like the one [documented previously](https://github.com/shlinkio/shlink-docker-image/tree/v1.16.3#provide-config-via-volumes).
|
||||||
> The `not_found_redirect_to` option has been deprecated when `404_redirect_to` and `base_url_redirect_to` have been introduced. Use `invalid_short_url_redirect_to` instead (however, it will still work for backwards compatibility).
|
> The `not_found_redirect_to` option has been deprecated when `regular_404_redirect_to` and `base_url_redirect_to` have been introduced. Use `invalid_short_url_redirect_to` instead (however, it will still work for backwards compatibility).
|
||||||
|
|
||||||
Once created just run shlink with the volume:
|
Once created just run shlink with the volume:
|
||||||
|
|
||||||
|
@ -95,7 +95,7 @@ $helper = new class {
|
|||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'invalid_short_url' => env('INVALID_SHORT_URL_REDIRECT_TO', env('NOT_FOUND_REDIRECT_TO')),
|
'invalid_short_url' => env('INVALID_SHORT_URL_REDIRECT_TO', env('NOT_FOUND_REDIRECT_TO')),
|
||||||
'404' => env('404_REDIRECT_TO'),
|
'regular_404' => env('REGULAR_404_REDIRECT_TO'),
|
||||||
'base_url' => env('BASE_URL_REDIRECT_TO'),
|
'base_url' => env('BASE_URL_REDIRECT_TO'),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,7 @@ return [
|
|||||||
|
|
||||||
Options\AppOptions::class => ConfigAbstractFactory::class,
|
Options\AppOptions::class => ConfigAbstractFactory::class,
|
||||||
Options\DeleteShortUrlsOptions::class => ConfigAbstractFactory::class,
|
Options\DeleteShortUrlsOptions::class => ConfigAbstractFactory::class,
|
||||||
Options\NotFoundShortUrlOptions::class => ConfigAbstractFactory::class,
|
Options\NotFoundRedirectOptions::class => ConfigAbstractFactory::class,
|
||||||
Options\UrlShortenerOptions::class => ConfigAbstractFactory::class,
|
Options\UrlShortenerOptions::class => ConfigAbstractFactory::class,
|
||||||
|
|
||||||
Service\UrlShortener::class => ConfigAbstractFactory::class,
|
Service\UrlShortener::class => ConfigAbstractFactory::class,
|
||||||
@ -44,7 +44,7 @@ return [
|
|||||||
|
|
||||||
Options\AppOptions::class => ['config.app_options'],
|
Options\AppOptions::class => ['config.app_options'],
|
||||||
Options\DeleteShortUrlsOptions::class => ['config.delete_short_urls'],
|
Options\DeleteShortUrlsOptions::class => ['config.delete_short_urls'],
|
||||||
Options\NotFoundShortUrlOptions::class => ['config.url_shortener.not_found_short_url'],
|
Options\NotFoundRedirectOptions::class => ['config.not_found_redirects'],
|
||||||
Options\UrlShortenerOptions::class => ['config.url_shortener'],
|
Options\UrlShortenerOptions::class => ['config.url_shortener'],
|
||||||
|
|
||||||
Service\UrlShortener::class => ['httpClient', 'em', Options\UrlShortenerOptions::class],
|
Service\UrlShortener::class => ['httpClient', 'em', Options\UrlShortenerOptions::class],
|
||||||
@ -58,7 +58,7 @@ return [
|
|||||||
Service\UrlShortener::class,
|
Service\UrlShortener::class,
|
||||||
Service\VisitsTracker::class,
|
Service\VisitsTracker::class,
|
||||||
Options\AppOptions::class,
|
Options\AppOptions::class,
|
||||||
Options\NotFoundShortUrlOptions::class,
|
Options\NotFoundRedirectOptions::class,
|
||||||
'Logger_Shlink',
|
'Logger_Shlink',
|
||||||
],
|
],
|
||||||
Action\PixelAction::class => [
|
Action\PixelAction::class => [
|
||||||
|
@ -18,18 +18,18 @@ class RedirectAction extends AbstractTrackingAction
|
|||||||
{
|
{
|
||||||
use ErrorResponseBuilderTrait;
|
use ErrorResponseBuilderTrait;
|
||||||
|
|
||||||
/** @var Options\NotFoundShortUrlOptions */
|
/** @var Options\NotFoundRedirectOptions */
|
||||||
private $notFoundOptions;
|
private $redirectOptions;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
UrlShortenerInterface $urlShortener,
|
UrlShortenerInterface $urlShortener,
|
||||||
VisitsTrackerInterface $visitTracker,
|
VisitsTrackerInterface $visitTracker,
|
||||||
Options\AppOptions $appOptions,
|
Options\AppOptions $appOptions,
|
||||||
Options\NotFoundShortUrlOptions $notFoundOptions,
|
Options\NotFoundRedirectOptions $redirectOptions,
|
||||||
?LoggerInterface $logger = null
|
?LoggerInterface $logger = null
|
||||||
) {
|
) {
|
||||||
parent::__construct($urlShortener, $visitTracker, $appOptions, $logger);
|
parent::__construct($urlShortener, $visitTracker, $appOptions, $logger);
|
||||||
$this->notFoundOptions = $notFoundOptions;
|
$this->redirectOptions = $redirectOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function createSuccessResp(string $longUrl): Response
|
protected function createSuccessResp(string $longUrl): Response
|
||||||
@ -43,8 +43,8 @@ class RedirectAction extends AbstractTrackingAction
|
|||||||
ServerRequestInterface $request,
|
ServerRequestInterface $request,
|
||||||
RequestHandlerInterface $handler
|
RequestHandlerInterface $handler
|
||||||
): Response {
|
): Response {
|
||||||
if ($this->notFoundOptions->isRedirectionEnabled()) {
|
if ($this->redirectOptions->hasInvalidShortUrlRedirect()) {
|
||||||
return new RedirectResponse($this->notFoundOptions->getRedirectTo());
|
return new RedirectResponse($this->redirectOptions->getInvalidShortUrlRedirect());
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->buildErrorResponse($request, $handler);
|
return $this->buildErrorResponse($request, $handler);
|
||||||
|
@ -24,7 +24,7 @@ class SimplifiedConfigParser
|
|||||||
'validate_url' => ['url_shortener', 'validate_url'],
|
'validate_url' => ['url_shortener', 'validate_url'],
|
||||||
'not_found_redirect_to' => ['not_found_redirects', 'invalid_short_url'], // Deprecated
|
'not_found_redirect_to' => ['not_found_redirects', 'invalid_short_url'], // Deprecated
|
||||||
'invalid_short_url_redirect_to' => ['not_found_redirects', 'invalid_short_url'],
|
'invalid_short_url_redirect_to' => ['not_found_redirects', 'invalid_short_url'],
|
||||||
'404_redirect_to' => ['not_found_redirects', '404'],
|
'regular_404_redirect_to' => ['not_found_redirects', 'regular_404'],
|
||||||
'base_url_redirect_to' => ['not_found_redirects', 'base_path'],
|
'base_url_redirect_to' => ['not_found_redirects', 'base_path'],
|
||||||
'db_config' => ['entity_manager', 'connection'],
|
'db_config' => ['entity_manager', 'connection'],
|
||||||
'delete_short_url_threshold' => ['delete_short_urls', 'visits_threshold'],
|
'delete_short_url_threshold' => ['delete_short_urls', 'visits_threshold'],
|
||||||
|
65
module/Core/src/Options/NotFoundRedirectOptions.php
Normal file
65
module/Core/src/Options/NotFoundRedirectOptions.php
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Shlinkio\Shlink\Core\Options;
|
||||||
|
|
||||||
|
use Zend\Stdlib\AbstractOptions;
|
||||||
|
|
||||||
|
class NotFoundRedirectOptions extends AbstractOptions
|
||||||
|
{
|
||||||
|
/** @var string|null */
|
||||||
|
private $invalidShortUrl;
|
||||||
|
/** @var string|null */
|
||||||
|
private $regular404;
|
||||||
|
/** @var string|null */
|
||||||
|
private $baseUrl;
|
||||||
|
|
||||||
|
public function getInvalidShortUrlRedirect(): ?string
|
||||||
|
{
|
||||||
|
return $this->invalidShortUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function hasInvalidShortUrlRedirect(): bool
|
||||||
|
{
|
||||||
|
return $this->invalidShortUrl !== null;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function setInvalidShortUrl(?string $invalidShortUrl): self
|
||||||
|
{
|
||||||
|
$this->invalidShortUrl = $invalidShortUrl;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getRegular404Redirect(): ?string
|
||||||
|
{
|
||||||
|
return $this->regular404;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function hasRegular404Redirect(): bool
|
||||||
|
{
|
||||||
|
return $this->regular404 !== null;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function setRegular404(?string $regular404): self
|
||||||
|
{
|
||||||
|
$this->regular404 = $regular404;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getBaseUrlRedirect(): ?string
|
||||||
|
{
|
||||||
|
return $this->baseUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function hasBaseUrlRedirect(): bool
|
||||||
|
{
|
||||||
|
return $this->baseUrl !== null;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function setBaseUrl(?string $baseUrl): self
|
||||||
|
{
|
||||||
|
$this->baseUrl = $baseUrl;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
}
|
@ -1,37 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Shlinkio\Shlink\Core\Options;
|
|
||||||
|
|
||||||
use Zend\Stdlib\AbstractOptions;
|
|
||||||
|
|
||||||
class NotFoundShortUrlOptions extends AbstractOptions
|
|
||||||
{
|
|
||||||
/** @var bool */
|
|
||||||
private $enableRedirection = false;
|
|
||||||
/** @var string|null */
|
|
||||||
private $redirectTo;
|
|
||||||
|
|
||||||
public function isRedirectionEnabled(): bool
|
|
||||||
{
|
|
||||||
return $this->enableRedirection;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function setEnableRedirection(bool $enableRedirection = true): self
|
|
||||||
{
|
|
||||||
$this->enableRedirection = $enableRedirection;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getRedirectTo(): string
|
|
||||||
{
|
|
||||||
return $this->redirectTo ?? '';
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function setRedirectTo(?string $redirectTo): self
|
|
||||||
{
|
|
||||||
$this->redirectTo = $redirectTo;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
}
|
|
@ -25,20 +25,20 @@ class RedirectActionTest extends TestCase
|
|||||||
private $urlShortener;
|
private $urlShortener;
|
||||||
/** @var ObjectProphecy */
|
/** @var ObjectProphecy */
|
||||||
private $visitTracker;
|
private $visitTracker;
|
||||||
/** @var Options\NotFoundShortUrlOptions */
|
/** @var Options\NotFoundRedirectOptions */
|
||||||
private $notFoundOptions;
|
private $redirectOptions;
|
||||||
|
|
||||||
public function setUp(): void
|
public function setUp(): void
|
||||||
{
|
{
|
||||||
$this->urlShortener = $this->prophesize(UrlShortener::class);
|
$this->urlShortener = $this->prophesize(UrlShortener::class);
|
||||||
$this->visitTracker = $this->prophesize(VisitsTracker::class);
|
$this->visitTracker = $this->prophesize(VisitsTracker::class);
|
||||||
$this->notFoundOptions = new Options\NotFoundShortUrlOptions();
|
$this->redirectOptions = new Options\NotFoundRedirectOptions();
|
||||||
|
|
||||||
$this->action = new RedirectAction(
|
$this->action = new RedirectAction(
|
||||||
$this->urlShortener->reveal(),
|
$this->urlShortener->reveal(),
|
||||||
$this->visitTracker->reveal(),
|
$this->visitTracker->reveal(),
|
||||||
new Options\AppOptions(['disableTrackParam' => 'foobar']),
|
new Options\AppOptions(['disableTrackParam' => 'foobar']),
|
||||||
$this->notFoundOptions
|
$this->redirectOptions
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,8 +89,7 @@ class RedirectActionTest extends TestCase
|
|||||||
$handler = $this->prophesize(RequestHandlerInterface::class);
|
$handler = $this->prophesize(RequestHandlerInterface::class);
|
||||||
$handle = $handler->handle(Argument::any())->willReturn(new Response());
|
$handle = $handler->handle(Argument::any())->willReturn(new Response());
|
||||||
|
|
||||||
$this->notFoundOptions->enableRedirection = true;
|
$this->redirectOptions->invalidShortUrl = 'https://shlink.io';
|
||||||
$this->notFoundOptions->redirectTo = 'https://shlink.io';
|
|
||||||
|
|
||||||
$request = (new ServerRequest())->withAttribute('shortCode', $shortCode);
|
$request = (new ServerRequest())->withAttribute('shortCode', $shortCode);
|
||||||
$resp = $this->action->process($request, $handler->reveal());
|
$resp = $this->action->process($request, $handler->reveal());
|
||||||
|
Loading…
Reference in New Issue
Block a user