diff --git a/src/Shortener.php b/src/Shortener.php index cf58595..aad8c02 100644 --- a/src/Shortener.php +++ b/src/Shortener.php @@ -22,6 +22,7 @@ use craft\web\UrlManager; use codemonauts\shortener\jobs\UpdateShortUrl; use yii\base\Event; +use yii\web\NotFoundHttpException; /** @@ -54,6 +55,13 @@ public function init() // Get settings $settings = $this->getSettings(); + // Check for root path in domain + $domain = Craft::parseEnv($settings->domain); + $request = Craft::$app->getRequest(); + if ($request->isSiteRequest && stripos($request->hostInfo, $domain) !== false && $request->getUrl() === '/') { + throw new NotFoundHttpException(); + } + // Register components $this->components = [ 'shortUrl' => ShortUrl::class, @@ -84,6 +92,7 @@ public function init() Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_SITE_URL_RULES, function(RegisterUrlRulesEvent $event) use ($settings) { if ($settings->domain !== '') { $event->rules['//' . Craft::parseEnv($settings->domain) . '/'] = 'shortener/redirect'; + $event->rules['//' . Craft::parseEnv($settings->domain) . ''] = 'shortener/redirect/catch-all'; } }); @@ -154,7 +163,7 @@ protected function createSettingsModel() protected function settingsHtml() { return Craft::$app->getView()->renderTemplate('shortener/settings', [ - 'settings' => $this->getSettings() + 'settings' => $this->getSettings(), ] ); } diff --git a/src/controllers/RedirectController.php b/src/controllers/RedirectController.php index 7ab2966..22eb3c9 100644 --- a/src/controllers/RedirectController.php +++ b/src/controllers/RedirectController.php @@ -24,4 +24,9 @@ public function actionRedirect(string $code) return $this->redirect($shortUrl->destination, $shortUrl->redirectCode); } + + public function actionCatchAll(string $path) + { + throw new NotFoundHttpException(); + } }