Ultimate Guide to Website Auto Redirects

I’ll share a real-life experience with you. I built a website and deployed it on Netlify.app, but later I moved it to my own custom domain. The problem was that the old Netlify URL had already been shared in many places, and I couldn’t update it everywhere. So, I decided to simply redirect all traffic from the Netlify site to my main domain — that way, anyone using the old link would still land on the right website.

This is where I learned about URL redirects and implemented a server-side redirect for my case.
In this article, let’s quickly explore the different types of URL redirects and how they work.

Why Do We Need Redirects?

Redirects are crucial for:

  • Domain Migration – moving from oldsite.com to newsite.com.

  • SEO Optimization – keeping your rankings safe.

  • User Experience – automatically sending users to the correct page.

  • Temporary Needs – like maintenance pages or login-required redirects.

Frontend Redirects

1. HTML Meta Refresh

<meta http-equiv="refresh" content="5; url=https://example.com">
  • Redirects after 5 seconds.

  • Use 0 for immediate redirect.

Note: Not SEO-friendly — search engines see it as a slow redirect.

2. JavaScript Redirect

<script>
  setTimeout(() => {
    window.location.href = "https://example.com";
  }, 5000);
</script>
  • Flexible — you can add conditions (like checking login status).

  • Use window.location.replace("https://example.com") if you don’t want users to go back.


Backend Redirects

1. Apache (.htaccess) Redirect

Redirect 301 / https://example.com
Redirect 301 /oldpage.html https://example.com/newpage.html
  • 301 → Permanent (SEO-friendly).

  • 302 → Temporary.

2. Nginx Redirect

server {
    listen 80;
    server_name oldsite.com;
    return 301 https://newsite.com;
}

Redirects entire domain to the new one.

3. Node.js / Express Redirect

app.get("/", (req, res) => {
  res.redirect(301, "https://example.com");
});
  • Default is 302 (temporary).

  • Use 301 for permanent.

Types of Redirects

CodeTypeUse CaseSEO Impact
301PermanentDomain/page migrationPasses SEO juice
302TemporaryMaintenance, A/B testingDoesn’t pass SEO juice
307Temporary (keeps method)Similar to 302 but preserves POST/GETSafe for specific cases
308Permanent (keeps method)Similar to 301 but preserves methodSEO-safe

Best Practices

  • Prefer server-side redirects for performance + SEO.

  • Always use 301 when migrating domains.

  • Avoid infinite redirect loops.

  • Test redirects with tools (Chrome DevTools → Network tab, or Redirect Checker).


Conclusion

  • Use HTML/JS redirects only for quick fixes.

  • For SEO and professional use, always rely on server-side (301/302) redirects.

  • Redirects, when done right, improve user experience and protect your SEO rankings.


Key Takeaway:

  • Frontend = quick but weak.

  • Backend = professional, SEO-safe, and faster.


I’ll be updating this article soon with real-world redirect examples (code + screenshots) so you can see redirects in action.

If you face any problems while configuring redirects, feel free to drop a comment below — I’ll be happy to help you out.

📩 Subscribe for more developer-friendly tutorials. Happy Coding! 💻

0
Subscribe to my newsletter

Read articles from Mohd Ahsan Raza Khan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mohd Ahsan Raza Khan
Mohd Ahsan Raza Khan

👋 Hi, I'm Mohd Ahsan Raza Khan! I know you might be thinking my name is quite long, right? Don't worry, you can call me MARK. I'm a software developer with a passion for building web applications and sharing knowledge through writing. I love exploring new technologies and frameworks, and I'm particularly interested in JavaScript, ReactJS, NodeJS, ExpressJS, and MongoDB. In my free time, I enjoy learning about new technologies and writing tutorials to help others learn and grow in their coding journey. I'm always excited to connect with fellow developers, so feel free to reach out to me on LinkedIn. Let's build something amazing together!