301 Redirect Code Generator

Move a page or a whole domain and keep your SEO with the right redirect code.

301 Moved Permanently

/old-page.html → https://www.example.com/new-page

Use the server-side snippet for your stack; the HTML and JavaScript versions are fallbacks.

Apache (.htaccess)
Redirect 301 /old-page.html https://www.example.com/new-page
Nginx
location = /old-page.html {
    return 301 https://www.example.com/new-page;
}
PHP
<?php
header("Location: https://www.example.com/new-page", true, 301);
exit;
IIS (web.config)
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Redirect" stopProcessing="true">
          <match url="^old-page\.html$" />
          <action type="Redirect" url="https://www.example.com/new-page" redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>
Node.js (Express)
app.get("/old-page.html", (req, res) => {
  res.redirect(301, "https://www.example.com/new-page");
});
Python (Flask)
# Flask
from flask import redirect

@app.route("/old-page.html")
def moved():
    return redirect("https://www.example.com/new-page", code=301)
HTML meta refresh (fallback)
<!-- Not a real 301: search engines treat an instant meta refresh similar to a redirect. -->
<meta http-equiv="refresh" content="0; url=https://www.example.com/new-page">
<link rel="canonical" href="https://www.example.com/new-page">
JavaScript (fallback)
<script>
  window.location.replace("https://www.example.com/new-page");
</script>

301 Redirect Code Generator

When you rename a page, restructure your site or move to a new domain, the old URLs should not return 404 errors. A 301 redirect forwards visitors and search engines to the new address and tells Google that the move is permanent, so links, bookmarks and most ranking signals carry over. The 301 Redirect Code Generator produces ready-to-paste code for the most common servers and frameworks from just two fields.

Supported platforms

  • Apache (.htaccess) – Redirect 301 for single pages, mod_rewrite for whole-site moves.
  • Nginx – an exact-match location block or a server block that returns 301 with $request_uri.
  • PHP – header("Location: …", true, 301) followed by exit.
  • IIS (web.config) – a URL Rewrite rule with redirectType="Permanent".
  • Node.js (Express) and Python (Flask) – application-level redirects.
  • HTML meta refresh and JavaScript – client-side fallbacks for static hosting where you cannot configure the server.

Single page vs. whole site

For a single page, enter the old path (or the full old URL — only the path is used) and the new URL. For a domain migration, switch on Redirect the whole site: every request is forwarded to the same path on the new domain, preserving deep links.

Redirect best practices

Redirect each old URL to its most relevant new page instead of sending everything to the homepage. Avoid chains (A → B → C) and loops, update internal links to point directly at the new URLs, and keep redirects in place for at least a year. Use a 302 only for genuinely temporary moves such as maintenance pages. To create clean target URLs use the Slug Generator, and to encode special characters in them use the URL Encoder.

Frequently Asked Questions

A 301 tells browsers and search engines the page has moved permanently, so they update their index and pass ranking signals to the new URL. A 302 is temporary: the old URL stays indexed. Use 301 for migrations and URL changes.

Use the one for the software that serves your site: .htaccess for most shared hosting with Apache, the Nginx block for Nginx servers, web.config for Windows/IIS, and the PHP, Express or Flask code if your application handles routing itself.

No. They run in the browser after a 200 response, so no 301 status is sent. Google usually follows an instant meta refresh, but server-side redirects are faster and more reliable. Use the HTML and JavaScript versions only when you cannot change the server.

Enable "Redirect the whole site". The generated rules forward every path to the same path on the new domain, e.g. old.com/blog/post to new.com/blog/post, which preserves deep links.