Improve pretty urls

This commit is contained in:
grandeljay 2022-02-26 03:14:23 +01:00
parent d970e41f27
commit ee622ea514
4 changed files with 46 additions and 19 deletions

View file

@ -1,10 +1,10 @@
RewriteEngine On RewriteEngine On
# Wishlists
RewriteRule ^([a-z\-]+)/(\d+)$ /?page=$1&wishlist=$2 [QSA,L]
# Wishlist
RewriteRule ^wishlist/([a-z0-9]+)$ /?wishlist=$1 [QSA,L]
# Page # Page
RewriteRule ^([a-z\-]+)$ /?page=$1 [QSA,L] RewriteRule ^([a-z\-]+)$ /?page=$1 [QSA,L]
# Wishlist
RewriteRule ^wishlist/([a-z0-9]+) /?wishlist=$1 [QSA,L]
# Wishlists
RewriteRule ^wishlist/(\d+) /?wishlist=$1 [QSA,L]

View file

@ -138,17 +138,41 @@ class Page
if (count($parts) >= 2) { if (count($parts) >= 2) {
switch ($parts[0]) { switch ($parts[0]) {
case 'RewriteRule': case 'RewriteRule':
$url = $parts[1]; $rewriteRule = $parts[1];
$url = ltrim($url, '^'); $rewriteRule = ltrim($rewriteRule, '^');
$url = rtrim($url, '$'); $rewriteRule = rtrim($rewriteRule, '$');
$url = $parts[2];
$keys = array_map(
function($item) {
return explode('=', $item)[0];
},
explode('&', parse_url($url, PHP_URL_QUERY))
);
$flags = explode(',', substr($parts[3], 1, -1)) ?? array(); $flags = explode(',', substr($parts[3], 1, -1)) ?? array();
preg_match('/\((.+?)\)/', $url, $regex); preg_match_all('/\(.+?\)/', $rewriteRule, $regexes);
if (isset($_GET)) { if (isset($_GET)) {
$countMatches = 0;
foreach ($regexes as $matches) {
foreach ($matches as $match) {
foreach ($_GET as $key => $value) { foreach ($_GET as $key => $value) {
if (preg_match('/^' . $regex[1] . '$/', $value)) { if (
$redirect_to = '/' . str_replace($regex[0], $value, $url); preg_match('/^' . $match . '$/', $value)
&& in_array($key, $keys)
&& count($_GET) === count($keys)
) {
$rewriteRule = str_replace($match, $value, $rewriteRule);
$countMatches++;
break;
}
}
}
if ($countMatches > 0 && $countMatches === count($matches)) {
$redirect_to = '/' . $rewriteRule;
if (in_array('L', $flags)) { if (in_array('L', $flags)) {
break 3; break 3;

View file

@ -35,11 +35,14 @@ class Wishlist
/** /**
* Get Wishlist * Get Wishlist
*/ */
$this->data = $database->query('SELECT * $result = $database
->query('SELECT *
FROM `wishlists` FROM `wishlists`
WHERE `' . $column . '` = ' . $id_or_hash . ';') WHERE `' . $column . '` = ' . $id_or_hash . ';')
->fetch(); ->fetch();
$this->data = $result ? $result : array();
/** Exists */ /** Exists */
if (isset($this->data['id'])) { if (isset($this->data['id'])) {
$this->id = $this->data['id']; $this->id = $this->data['id'];

View file

@ -8,7 +8,7 @@
use wishthis\{Page, User, Wishlist}; use wishthis\{Page, User, Wishlist};
$wishlist = new Wishlist(intval($_GET['wishlist'])); $wishlist = new Wishlist($_GET['wishlist']);
if (!$wishlist->exists) { if (!$wishlist->exists) {
http_response_code(404); http_response_code(404);