|
22 | 22 | use craft\web\UrlManager; |
23 | 23 | use codemonauts\shortener\jobs\UpdateShortUrl; |
24 | 24 | use yii\base\Event; |
| 25 | +use yii\web\NotFoundHttpException; |
25 | 26 |
|
26 | 27 |
|
27 | 28 | /** |
@@ -54,6 +55,13 @@ public function init() |
54 | 55 | // Get settings |
55 | 56 | $settings = $this->getSettings(); |
56 | 57 |
|
| 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 | + |
57 | 65 | // Register components |
58 | 66 | $this->components = [ |
59 | 67 | 'shortUrl' => ShortUrl::class, |
@@ -84,6 +92,7 @@ public function init() |
84 | 92 | Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_SITE_URL_RULES, function(RegisterUrlRulesEvent $event) use ($settings) { |
85 | 93 | if ($settings->domain !== '') { |
86 | 94 | $event->rules['//' . Craft::parseEnv($settings->domain) . '/<code:\w+>'] = 'shortener/redirect'; |
| 95 | + $event->rules['//' . Craft::parseEnv($settings->domain) . '<path:.*>'] = 'shortener/redirect/catch-all'; |
87 | 96 | } |
88 | 97 | }); |
89 | 98 |
|
@@ -154,7 +163,7 @@ protected function createSettingsModel() |
154 | 163 | protected function settingsHtml() |
155 | 164 | { |
156 | 165 | return Craft::$app->getView()->renderTemplate('shortener/settings', [ |
157 | | - 'settings' => $this->getSettings() |
| 166 | + 'settings' => $this->getSettings(), |
158 | 167 | ] |
159 | 168 | ); |
160 | 169 | } |
|
0 commit comments