DEV Community

Cover image for What’s the Difference Between Redirect vs Rewrite
Athreya aka Maneshwar
Athreya aka Maneshwar

Posted on

What’s the Difference Between Redirect vs Rewrite

Hello, I'm Maneshwar. I'm working on FreeDevTools online currently building **one place for all dev tools, cheat codes, and TLDRs* — a free, open-source hub where developers can quickly find and use tools without any hassle of searching all over the internet.

When managing a website, you often need to handle URLs carefully.

Two common mechanisms to manage URLs are redirects and rewrites.

Though they may seem similar, they serve very different purposes and have different implications for user experience, SEO, and website architecture.

This guide breaks it down in detail.

1. What is a Redirect?

A redirect tells the browser and search engines that a URL has moved from one location to another. When a redirect is in place:

  • The URL in the browser changes.
  • The server responds with a status code (most commonly 301 for permanent redirects or 302/307 for temporary ones).
  • Search engines are instructed to index the new URL instead of the old one.

Example

Suppose you moved a blog post from /old-blog-post to /new-blog-post. Using a 301 redirect:

location /old-blog-post {
    return 301 /new-blog-post;
}
Enter fullscreen mode Exit fullscreen mode
  • Visiting /old-blog-post automatically sends the user to /new-blog-post.
  • Google and other search engines will transfer ranking signals to the new URL.

Use Cases for Redirects

  1. Page moved permanently: Consolidating duplicate content.
  2. Domain migration: Moving from example.comnewdomain.com.
  3. Old URLs for SEO: Ensuring old links still bring users to the correct page.

2. What is a Rewrite?

A rewrite changes the URL internally on the server without changing what the user sees in their browser. This allows a URL to serve content from a different location on the server while keeping the original URL visible.

Example

You have a file about.html, but you want the URL to be /about:

location / {
    rewrite ^/about$ /about.html break;
    proxy_pass http://127.0.0.1:2368;
}
Enter fullscreen mode Exit fullscreen mode
  • Visiting /about will fetch the content of /about.html.
  • The URL in the browser remains /about.
  • Users and search engines see /about, not /about.html.

Use Cases for Rewrites

  1. Clean URLs: /about instead of /about.html.
  2. Single Page Apps (SPA): All routes point to index.html internally.
  3. Internal restructuring: You move files on the server without breaking public URLs.
  4. Serving content from multiple paths: For example, serving /blog/post from /content/posts/post.html.

3. Key Differences Between Redirect and Rewrite

Feature Redirect Rewrite
Browser URL Changes to the new URL Stays the same
HTTP Status Code 301, 302, 307 200 OK (server serves the rewritten content)
SEO Impact Transfers link equity (301) Original URL stays indexed
User Experience User is sent to a new URL URL looks unchanged
Performance Extra round-trip to the new URL Handled internally on the server
Typical Use Page moved, canonical URLs Pretty URLs, SPA routing, internal file restructuring

4. When to Use Which

Use a Redirect When:

  • You permanently moved a page and want search engines to index the new location (301).
  • You want to consolidate duplicate content under one URL.
  • You are migrating a domain or changing URL structure.

Use a Rewrite When:

  • You want a “pretty” URL for users but the content lives somewhere else.
  • You’re implementing SPA routing where multiple paths point to the same page.
  • You want internal flexibility without affecting the public URL.

5. Hybrid Approach (Best Practice for SEO)

Sometimes, the best approach combines both:

  1. Rewrite internally to serve content flexibly.
  2. Redirect externally to enforce canonical URLs for SEO.

Example:

  • You have an old blog /my-post-old and a new blog /my-post.
  • Users who type /my-post-old should see the new content (rewrite internally if needed).
  • Search engines should index /my-post as canonical (301 redirect or <link rel="canonical"> tag).
# Redirect old blog to new
rewrite ^/my-post-old$ /my-post permanent;

# Internal rewrite for clean URLs
rewrite ^/blog/(.*)$ /blog/index.php?post=$1 break;
Enter fullscreen mode Exit fullscreen mode

6. Real-World Examples

Scenario 1: Migrating a Blog

  • Old URL: /blog/my-old-post
  • New URL: /articles/my-old-post

Redirect (301):

rewrite ^/blog/my-old-post$ /articles/my-old-post permanent;
Enter fullscreen mode Exit fullscreen mode
  • Google updates its index.
  • Users visiting old links are sent to the new post.

Scenario 2: SPA Routing

  • URL /dashboard/settings should serve the same SPA page.

Rewrite internally:

location / {
    try_files $uri /index.html;
}
Enter fullscreen mode Exit fullscreen mode
  • Browser URL stays /dashboard/settings.
  • SPA handles routing internally.

Conclusion

  • Redirects are for moving URLs permanently or temporarily. They change the browser URL and inform search engines.
  • Rewrites are for serving content flexibly without changing the browser URL.
  • For SEO, use redirects for old URLs that need consolidation and rewrites for clean URLs or SPA routing.
  • Often, a hybrid approach (rewrite internally + canonical or 301 redirect) provides both a smooth user experience and strong SEO.

This approach keeps your website friendly for users, flexible for developers, and optimized for search engines.

FreeDevTools

I’ve been building for FreeDevTools.

A collection of UI/UX-focused tools crafted to simplify workflows, save time, and reduce friction in searching tools/materials.

Any feedback or contributors are welcome!

It’s online, open-source, and ready for anyone to use.

👉 Check it out: FreeDevTools
⭐ Star it on GitHub: freedevtools

Let’s make it even better together.

Top comments (0)