Improve pretty URLs

This commit is contained in:
grandeljay 2022-02-26 15:59:59 +01:00
parent ee622ea514
commit 2adc8e1bfc
3 changed files with 50 additions and 28 deletions

View file

@ -10,7 +10,7 @@ if ('serviceWorker' in navigator) {
}) })
} }
const urlParams = new URLSearchParams(window.location.search); const urlParams = new URLSearchParams($_GET);
$(function() { $(function() {
/** /**
@ -40,8 +40,8 @@ $(function() {
placeholder: 'No wishlist selected.' placeholder: 'No wishlist selected.'
}) })
if (urlParams.has('wishlist')) { if ($_GET.wishlist) {
element.dropdown('set selected', urlParams.get('wishlist')); element.dropdown('set selected', $_GET.wishlist);
} else { } else {
if (response.results[0]) { if (response.results[0]) {
element.dropdown('set selected', response.results[0].value); element.dropdown('set selected', response.results[0].value);

View file

@ -17,8 +17,8 @@ $(function () {
placeholder: 'No wishlist selected.' placeholder: 'No wishlist selected.'
}) })
if (urlParams.has('wishlist')) { if ($_GET.wishlist) {
element.dropdown('set selected', urlParams.get('wishlist')); element.dropdown('set selected', $_GET.wishlist);
} else { } else {
if (wishlists[0]) { if (wishlists[0]) {
element.dropdown('set selected', wishlists[0].value); element.dropdown('set selected', wishlists[0].value);
@ -45,7 +45,9 @@ $(function () {
$('[name="wishlist_delete_id"]').val(wishlistValue); $('[name="wishlist_delete_id"]').val(wishlistValue);
if (wishlistValue) { if (wishlistValue) {
$_GET.wishlist = wishlistValue;
urlParams.set('wishlist', wishlistValue); urlParams.set('wishlist', wishlistValue);
window.history.pushState({}, '', '/?' + urlParams.toString()); window.history.pushState({}, '', '/?' + urlParams.toString());
$('.wishlist-share').attr('href', '/?wishlist=' + wishlists[wishlistIndex].hash); $('.wishlist-share').attr('href', '/?wishlist=' + wishlists[wishlistIndex].hash);

View file

@ -129,6 +129,15 @@ class Page
/** /**
* Redirect * Redirect
*/ */
$redirect_to = $this->getPrettyURL($_SERVER['QUERY_STRING']);
if ($redirect_to && $redirect_to !== $_SERVER['REQUEST_URI']) {
header('Location: ' . $redirect_to);
die();
}
}
public function getPrettyURL(string $forURL): string {
$htaccess = explode(PHP_EOL, file_get_contents('.htaccess')); $htaccess = explode(PHP_EOL, file_get_contents('.htaccess'));
$redirect_to = ''; $redirect_to = '';
@ -150,33 +159,42 @@ class Page
); );
$flags = explode(',', substr($parts[3], 1, -1)) ?? array(); $flags = explode(',', substr($parts[3], 1, -1)) ?? array();
$parameters_pairs = explode('&', parse_url($forURL, PHP_URL_PATH));
$parameters = array();
foreach ($parameters_pairs as $index => $pair) {
$parts = explode('=', $pair);
$key = $parts[0];
$value = $parts[1];
$parameters[$key] = $value;
}
preg_match_all('/\(.+?\)/', $rewriteRule, $regexes); preg_match_all('/\(.+?\)/', $rewriteRule, $regexes);
if (isset($_GET)) { $countMatches = 0;
$countMatches = 0;
foreach ($regexes as $matches) { foreach ($regexes as $matches) {
foreach ($matches as $match) { foreach ($matches as $match) {
foreach ($_GET as $key => $value) { foreach ($parameters as $key => $value) {
if ( if (
preg_match('/^' . $match . '$/', $value) preg_match('/^' . $match . '$/', $value)
&& in_array($key, $keys) && in_array($key, $keys)
&& count($_GET) === count($keys) && count($parameters) === count($keys)
) { ) {
$rewriteRule = str_replace($match, $value, $rewriteRule); $rewriteRule = str_replace($match, $value, $rewriteRule);
$countMatches++; $countMatches++;
break; break;
}
} }
} }
}
if ($countMatches > 0 && $countMatches === count($matches)) { if ($countMatches > 0 && $countMatches === count($matches)) {
$redirect_to = '/' . $rewriteRule; $redirect_to = '/' . $rewriteRule;
if (in_array('L', $flags)) { if (in_array('L', $flags)) {
break 3; break 3;
}
} }
} }
} }
@ -185,10 +203,7 @@ class Page
} }
} }
if ($redirect_to && $redirect_to !== $_SERVER['REQUEST_URI']) { return $redirect_to;
header('Location: ' . $redirect_to);
die();
}
} }
public function header(): void public function header(): void
@ -240,6 +255,11 @@ class Page
/** /**
* Scripts * Scripts
*/ */
?>
<script type="text/javascript">
var $_GET = JSON.parse('<?= isset($_GET) ? json_encode($_GET) : array() ?>');
</script>
<?php
/** jQuery */ /** jQuery */
$scriptjQuery = 'node_modules/jquery/dist/jquery.min.js'; $scriptjQuery = 'node_modules/jquery/dist/jquery.min.js';