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'; import Layout from '../layouts/Layout.astro';
// 1. Capture the exact path the user tried to visit (e.g., "/hihi") // 1. Get the path from the URL params
const currentPath = Astro.url.pathname; const { path } = Astro.params;
// 2. Check for our loop-breaker
const isLooping = Astro.url.searchParams.get('noloop') === 'true'; const isLooping = Astro.url.searchParams.get('noloop') === 'true';
// 2. Build the correct shortlink URL without the "www." // 3. Define the shortlink target (ensuring we don't double the slash)
const shortlinkUrl = `https://upiecep.org${currentPath}`; const shortlinkUrl = `https://upiecep.org/${path || ''}`;
// 3. SERVER-SIDE REDIRECT LOGIC // 4. SERVER-SIDE REDIRECT LOGIC
// If 'noloop' is NOT present, it means the user came here directly (e.g. via www). // If we didn't come from Short.io (noloop is missing), bounce back to the shortener once.
// We redirect them to the short domain to see if a link exists there.
if (!isLooping) { if (!isLooping) {
return Astro.redirect(shortlinkUrl); return Astro.redirect(shortlinkUrl);
} }
// If we are here, it means isLooping is TRUE. // 5. If we reach here, it's a confirmed 404 (Short.io already checked).
// Short.io already checked and didn't find a link, so we show the 404 page. // Manually set the response status to 404 for SEO purposes.
Astro.response.status = 404;
--- ---
<Layout title="Page Not Found | UP IECEP"> <Layout title="Page Not Found | UP IECEP">
@@ -26,12 +28,11 @@ if (!isLooping) {
<p> <p>
We checked both the website and our shortlink database, but 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> </p>
<div class="button-group"> <div class="button-group">
<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 -->
<a href={shortlinkUrl} class="btn-shortlink">Try {shortlinkUrl}</a> <a href={shortlinkUrl} class="btn-shortlink">Try {shortlinkUrl}</a>
</div> </div>
</section> </section>