Fixed shortlink redirections

This commit is contained in:
Kent Joseph T. Palima
2026-05-04 11:39:05 +08:00
parent c444e4fea7
commit 52f528829e
+16 -10
View File
@@ -3,30 +3,36 @@ import Layout from '../layouts/Layout.astro';
// 1. Capture the exact path the user tried to visit (e.g., "/hihi") // 1. Capture the exact path the user tried to visit (e.g., "/hihi")
const currentPath = Astro.url.pathname; const currentPath = Astro.url.pathname;
const isLooping = Astro.url.searchParams.get('noloop') === 'true';
// 2. Build the correct shortlink URL without the "www." // 2. Build the correct shortlink URL without the "www."
const shortlinkUrl = `https://upiecep.org${currentPath}`; const shortlinkUrl = `https://upiecep.org${currentPath}`;
// 3. SERVER-SIDE REDIRECT LOGIC
// If 'noloop' is NOT present, it means the user came here directly (e.g. via www).
// We redirect them to the short domain to see if a link exists there.
if (!isLooping) {
return Astro.redirect(shortlinkUrl);
}
// If we are here, it means isLooping is TRUE.
// Short.io already checked and didn't find a link, so we show the 404 page.
--- ---
<Layout title="Page Not Found | UP IECEP"> <Layout title="Page Not Found | UP IECEP">
<section class="not-found-section"> <section class="not-found-section">
<h1>404</h1> <h1>404</h1>
<h2>Oops! We couldn't find that page.</h2> <h2>Oops! We couldn't find that page.</h2>
<p> <p>
The link you clicked might be broken, or the page may have been removed. We checked both the website and our shortlink database, but
</p> <strong>{currentPath}</strong> doesn't seem to exist.
<p class="shortlink-hint">
<strong>Looking for a shortlink?</strong> If you typed "www" before the link, it might have broken it. Try the direct shortlink below!
</p> </p>
<div class="button-group"> <div class="button-group">
<!-- Button 1: Back to Homepage -->
<a href="/" class="btn-main">Go to Homepage</a> <a href="/" class="btn-main">Go to Homepage</a>
<!-- We still keep this just in case they want to try again manually -->
<!-- Button 2: Dynamic Shortlink Attempt --> <a href={shortlinkUrl} class="btn-shortlink">Try {shortlinkUrl}</a>
<a href={shortlinkUrl} class="btn-shortlink">
Try {shortlinkUrl}
</a>
</div> </div>
</section> </section>
</Layout> </Layout>