Skip to content

Commit ee39054

Browse files
committed
add redirect for non-codes and root path
1 parent ea63db0 commit ee39054

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/Shortener.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use craft\web\UrlManager;
2323
use codemonauts\shortener\jobs\UpdateShortUrl;
2424
use yii\base\Event;
25+
use yii\web\NotFoundHttpException;
2526

2627

2728
/**
@@ -54,6 +55,13 @@ public function init()
5455
// Get settings
5556
$settings = $this->getSettings();
5657

58+
// Check for root path in domain
59+
$domain = Craft::parseEnv($settings->domain);
60+
$request = Craft::$app->getRequest();
61+
if ($request->isSiteRequest && stripos($request->hostInfo, $domain) !== false && $request->getUrl() === '/') {
62+
throw new NotFoundHttpException();
63+
}
64+
5765
// Register components
5866
$this->components = [
5967
'shortUrl' => ShortUrl::class,
@@ -84,6 +92,7 @@ public function init()
8492
Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_SITE_URL_RULES, function(RegisterUrlRulesEvent $event) use ($settings) {
8593
if ($settings->domain !== '') {
8694
$event->rules['//' . Craft::parseEnv($settings->domain) . '/<code:\w+>'] = 'shortener/redirect';
95+
$event->rules['//' . Craft::parseEnv($settings->domain) . '<path:.*>'] = 'shortener/redirect/catch-all';
8796
}
8897
});
8998

@@ -154,7 +163,7 @@ protected function createSettingsModel()
154163
protected function settingsHtml()
155164
{
156165
return Craft::$app->getView()->renderTemplate('shortener/settings', [
157-
'settings' => $this->getSettings()
166+
'settings' => $this->getSettings(),
158167
]
159168
);
160169
}

src/controllers/RedirectController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,9 @@ public function actionRedirect(string $code)
2424

2525
return $this->redirect($shortUrl->destination, $shortUrl->redirectCode);
2626
}
27+
28+
public function actionCatchAll(string $path)
29+
{
30+
throw new NotFoundHttpException();
31+
}
2732
}

0 commit comments

Comments
 (0)