Fixing shortlink redirection

This commit is contained in:
Kent Joseph T. Palima
2026-05-04 11:45:27 +08:00
parent 52f528829e
commit d33be6dbde
@@ -1,22 +1,24 @@
---
import Layout from '../layouts/Layout.astro';
// 1. Capture the exact path the user tried to visit (e.g., "/hihi")
const currentPath = Astro.url.pathname;
// 1. Get the path from the URL params
const { path } = Astro.params;
// 2. Check for our loop-breaker
const isLooping = Astro.url.searchParams.get('noloop') === 'true';
// 2. Build the correct shortlink URL without the "www."
const shortlinkUrl = `https://upiecep.org${currentPath}`;
// 3. Define the shortlink target (ensuring we don't double the slash)
const shortlinkUrl = `https://upiecep.org/${path || ''}`;
// 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.
// 4. SERVER-SIDE REDIRECT LOGIC
// If we didn't come from Short.io (noloop is missing), bounce back to the shortener once.
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.
// 5. If we reach here, it's a confirmed 404 (Short.io already checked).
// Manually set the response status to 404 for SEO purposes.
Astro.response.status = 404;
---
<Layout title="Page Not Found | UP IECEP">
@@ -26,12 +28,11 @@ if (!isLooping) {
<p>
We checked both the website and our shortlink database, but
<strong>{currentPath}</strong> doesn't seem to exist.
<strong>/{path}</strong> doesn't seem to exist.
</p>
<div class="button-group">
<a href="/" class="btn-main">Go to Homepage</a>
<!-- We still keep this just in case they want to try again manually -->
<a href={shortlinkUrl} class="btn-shortlink">Try {shortlinkUrl}</a>
</div>
</section>