How to Set Up Redirects and Rewrites for a Static Site

URL redirects and rewrites are essential for handling site restructuring, legacy URLs, and clean URL patterns on a static site — this guide covers implementing both effectively with Nginx.

Redirects vs Rewrites: The Key Difference

A redirect tells the browser "go to this different URL instead" (visible URL change, an actual round-trip); a rewrite serves different content for a URL internally without the browser knowing/URL changing — different use cases, and conflating them causes confusion.

Setting Up 301 Redirects for Moved Content

location = /old-page.html {
    return 301 /new-page.html;
}

A 301 (permanent redirect) tells search engines and browsers this content has permanently moved — important for preserving SEO value when restructuring content, similar in spirit to general redirect map practices.

Redirecting an Entire Old Path Structure

location ~ ^/blog/2023/(.*)$ {
    return 301 /articles/$1;
}

Pattern-based redirects handle bulk URL structure changes without needing individual redirect rules for every single affected page.

Setting Up Clean URLs (Rewriting .html Extensions Away)

location / {
    try_files $uri $uri.html $uri/ =404;
}

Allows /about to serve /about.html without the extension appearing in the URL or requiring a visible redirect — a rewrite, not a redirect, since the URL the visitor sees doesn't change.

Handling Trailing Slash Consistency

rewrite ^/(.*)/$ /$1 permanent;

Choose one consistent convention (with or without trailing slash) and redirect the other to it — avoids duplicate content issues from the same page being accessible at two different URL forms.

Redirecting Non-WWW to WWW (or Vice Versa)

server {
    server_name olddomain.com;
    return 301 https://www.yourdomain.com$request_uri;
}

Building a Redirect Map for a Major Site Migration

See redirect map practices discussed in broader site migration contexts — for significant restructuring affecting many URLs, build a systematic mapping (old URL to new URL) rather than ad-hoc individual rules, ensuring comprehensive coverage and easier maintenance.

Testing Redirects After Deployment

curl -I https://yourdomain.com/old-page.html

Verify the correct status code (301) and target location header, confirming redirects behave as intended before relying on them in production.

Avoiding Redirect Chains

A redirects to B redirects to C creates unnecessary extra round-trips and can confuse search engine crawlers — where possible, update redirects to point directly to the final destination rather than accumulating chains over successive restructurings.

Common Errors

Redirect works in browser but search engines still show old URL — search engine re-crawling and index updates take time even after correct 301 redirects are in place; this is expected latency, not necessarily a configuration problem.

Continue Reading

Browse more articles in Static Site Hosting & Frontend Deployment.

  • nginx redirects static site, 301 redirect vs rewrite, clean urls nginx static site, url rewrite static hosting
  • 0 användare blev hjälpta av detta svar
Hjälpte svaret dig?

Relaterade artiklar

How to Host a Static Website on a VPS with Nginx

Static websites — plain HTML, CSS, and JavaScript with no server-side processing —...

How to Deploy a Next.js Application on a VPS

Next.js supports several deployment modes — fully static export, server-side rendering with...

How to Deploy a Static Site Built with Astro, Hugo, or Jekyll

Static site generators (Astro, Hugo, Jekyll) produce plain HTML/CSS/JS at build time —...

How to Optimize Images for Web Performance

Images are typically the largest contributor to page weight and load time. This guide covers...

How to Set Up a Jamstack Site with a Headless CMS Backend

The Jamstack architecture (JavaScript, APIs, Markup) combines a pre-built static frontend with a...