From 1e935123c1f46de30372e3e7e80dc339f9e05c41 Mon Sep 17 00:00:00 2001 From: grandeljay Date: Sat, 28 May 2022 19:11:11 +0200 Subject: [PATCH] Refactor URLs --- .htaccess | 15 +++++++++------ index.php | 7 ------- src/assets/js/wishlists.js | 4 ++-- src/classes/page.php | 2 +- src/classes/url.php | 13 ++++++++++--- src/pages/wishlist.php | 2 +- 6 files changed, 23 insertions(+), 20 deletions(-) diff --git a/.htaccess b/.htaccess index ca2e1a5f..5e707f88 100644 --- a/.htaccess +++ b/.htaccess @@ -4,14 +4,17 @@ RewriteEngine On - # Wishlists / Wish - RewriteRule ^([a-z\-]+)/(\d+)$ /?page=$1&id=$2 [QSA,L] - - # Wishlist - RewriteRule ^wishlist/([a-z0-9]+)$ /?wishlist=$1 [QSA,L] - # Page RewriteRule ^([a-z\-]+)$ /?page=$1 [QSA,L] + + # Wishlists (My lists) + RewriteRule ^(wishlists)/([0-9]+)$ /?page=$1&id=$2 [QSA,L] + + # Wish + RewriteRule ^(wish)/(\d+)$ /?page=$1&id=$2 [QSA,L] + + # Wishlist + RewriteRule ^(wishlist)/([0-9a-f]{40})$ /?page=$1&hash=$2 [QSA,L] ##-- When caching of gzipped JS and CSS files is used, enable this setting diff --git a/index.php b/index.php index a5fde097..5f66e276 100644 --- a/index.php +++ b/index.php @@ -148,13 +148,6 @@ if ($options && $options->getOption('isInstalled') && !(defined('ENV_IS_DEV') && } } -/** - * Wishlist - */ -if (!isset($_SESSION['_GET']['page']) && isset($_SESSION['_GET']['wishlist'])) { - $page = 'wishlist'; -} - /** * Page */ diff --git a/src/assets/js/wishlists.js b/src/assets/js/wishlists.js index f177374f..26ccdb36 100644 --- a/src/assets/js/wishlists.js +++ b/src/assets/js/wishlists.js @@ -49,7 +49,7 @@ $(function () { if (wishlistValue) { wishlist.id = wishlistValue; - $('.wishlist-share').attr('href', '/?wishlist=' + wishlists[wishlistIndex].hash); + $('.wishlist-share').attr('href', '/?page=wishlist&hash=' + wishlists[wishlistIndex].hash); $('.button.wishlist-wish-add').removeClass('disabled'); $('.button.wishlist-share').removeClass('disabled'); @@ -65,7 +65,7 @@ $(function () { .then(handleFetchError) .then(handleFetchResponse) .then(function(response) { - window.history.pushState({}, '', response.data.url); + window.history.replaceState(null, document.title, response.data.url); $('.ui.dropdown.filter.priority') .dropdown('restore default value') diff --git a/src/classes/page.php b/src/classes/page.php index 3f809026..e5a3a676 100644 --- a/src/classes/page.php +++ b/src/classes/page.php @@ -164,7 +164,7 @@ class Page if ($options && $options->getOption('isInstalled') && isset($_SESSION['_GET'])) { $url = new URL(http_build_query($_SESSION['_GET'])); - if ($url->isPretty()) { + if (false === $url->isPretty()) { redirect($url->getPretty()); } } diff --git a/src/classes/url.php b/src/classes/url.php index 76ece6d4..28149e69 100644 --- a/src/classes/url.php +++ b/src/classes/url.php @@ -10,13 +10,18 @@ namespace wishthis; class URL { - public function __construct(private string $url) + public string $url; + + public function __construct(string $url) { + $this->url = urldecode($url); } public function isPretty(): bool { - return preg_match('/^\/\?.+?=.+?$/', $this->url); + $isPretty = 1 === preg_match('/^\/[a-z0-9\/]+/', $this->url); + + return $isPretty; } public function getPermalink(): string @@ -95,6 +100,8 @@ class URL ) { $rewriteRule = str_replace($match, $value, $rewriteRule); + error_log('Successfully matched ' . $match . ' with ' . $value . ' New rule is now: ' . $rewriteRule); + $countMatches++; break; } @@ -114,6 +121,6 @@ class URL } } - return $pretty_url ?: '?' . $this->url; + return $pretty_url ?: '/?' . $this->url; } } diff --git a/src/pages/wishlist.php b/src/pages/wishlist.php index 5fd31d91..0c9a4407 100644 --- a/src/pages/wishlist.php +++ b/src/pages/wishlist.php @@ -8,7 +8,7 @@ namespace wishthis; -$wishlist = new Wishlist($_SESSION['_GET']['wishlist']); +$wishlist = new Wishlist($_SESSION['_GET']['hash']); $page = new Page(__FILE__, $wishlist->getTitle()); if (!$wishlist->exists) {