How to do a 301 redirect in .htaccess, PHP and Nginx

By Huzaifa Awais ·

A 301 redirect tells browsers and Google that a page has moved for good. Add Redirect 301 /old /new to .htaccess, send a Location header with status 301 in PHP, or use return 301 in Nginx.

This guide gives you copy ready code for all three, shows how to test it, and lists the mistakes that cost rankings. If you have a long list of URLs, the free 301 redirect generator writes the rules for you.

What is a 301 redirect?

A 301 redirect is a server response that means “moved permanently”. Visitors land on the new URL, and search engines transfer the old page’s rankings and links to it.

The number is the HTTP status code. When a browser asks for the old URL, the server answers with 301 Moved Permanently and a Location header that holds the new address. The browser then loads that address on its own.

When should you use a 301?

  • You changed a page’s URL or slug.
  • You moved to a new domain.
  • You switched from HTTP to HTTPS, or from www to non www.
  • You merged two pages into one.
  • You deleted a page and a close replacement exists.

How do I do a 301 redirect in .htaccess?

Add Redirect 301 /old-page.html https://example.com/new-page/ to the .htaccess file in your site root. It works on Apache, cPanel and most shared hosting.

Redirect one page

Redirect 301 /old-page.html https://example.com/new-page/

The first path is relative to your site root and starts with a slash. The second is the full new URL.

Redirect with mod_rewrite

Use mod_rewrite when you need patterns, or when the site already uses RewriteRule (WordPress and Laravel do).

RewriteEngine On
RewriteRule ^old-page\.html$ /new-page/ [R=301,L]

In .htaccess the pattern has no leading slash. ^ and $ make it an exact match, and the backslash escapes the dot.

Force HTTPS and non www in one rule

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [L,R=301]

Replace example.com with your domain. Doing both in one rule avoids a chain of two redirects.

Why is my 301 redirect not working in .htaccess?

Usually because Apache is not reading the file, mod_rewrite is off, another rule runs first, or your browser is showing a cached redirect.

Work through these in order. Test each change in a private window or with curl -I, because browsers cache 301 redirects for a long time.

  1. The file is not being read. It must be named exactly .htaccess (with the dot, no .txt) and sit in the folder the URL points to, usually public_html. Put a deliberate typo on the first line: if the site does not show a 500 error, Apache is ignoring the file. On your own server, set AllowOverride All for that directory.
  2. mod_rewrite is off. RewriteRule lines need RewriteEngine On above them and the rewrite module enabled. Redirect 301 lines do not need mod_rewrite, so try one to tell the two apart.
  3. A leading slash in the pattern. In .htaccess, RewriteRule ^/old-page$ never matches. Drop the slash: RewriteRule ^old-page$.
  4. Another rule catches the URL first. WordPress and Laravel send every request to index.php. Put your redirects above the BEGIN WordPress block or the framework's rewrite rules.
  5. Mixing Redirect and RewriteRule. They are handled by different modules and the order between them is not guaranteed. Use one style in a file.
  6. Query strings. RewriteRule never sees the part after ?. Match it with RewriteCond %{QUERY_STRING}, and add QSD to drop it from the new URL.
  7. The server is not Apache. Nginx ignores .htaccess completely; use the Nginx rules below. LiteSpeed reads .htaccess but caches it, so restart or wait a minute after editing.

To see exactly what the server sends, run:

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

A working redirect answers HTTP/1.1 301 Moved Permanently with a Location: line pointing to the new URL. A 302 means the rule is missing R=301, and a 200 means the rule did not match.

How do I do a 301 redirect in PHP?

Call header('Location: https://example.com/new-page/', true, 301); before any output, then call exit.

<?php
header('Location: https://example.com/new-page/', true, 301);
exit;

Why the exit matters

Without exit, the rest of the script keeps running after the header is sent. That wastes work, and on some pages it can leak content to bots that ignore redirects.

Fixing “headers already sent”

PHP can only send headers before the first byte of output. A blank line before <?php, an echo, or a file saved with a byte order mark will all trigger this error. Move the redirect to the very top of the file.

How do I do a 301 redirect in Nginx?

Add location = /old-page { return 301 /new-page/; } inside your server block, then run nginx -t and reload.

Redirect one page

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

Redirect a whole domain

server {
    listen 80;
    server_name old-domain.com www.old-domain.com;
    return 301 https://new-domain.com$request_uri;
}

$request_uri keeps the path and query string, so every old page lands on its matching new page. Nginx ignores .htaccess files completely, so rules must go in the server config.

What is the difference between a 301 and a 302 redirect?

A 301 is permanent and a 302 is temporary. Search engines index the new URL after a 301, but keep the old URL after a 302.

CodeMeaningUse it when
301Moved permanentlyA page or site has moved for good
302Found (temporary)A short promotion or maintenance page
307Temporary, method keptA temporary move of a form or API endpoint
308Permanent, method keptA permanent move of a form or API endpoint

Browsers may turn a POST into a GET after a 301. If a form posts to the old URL, use 308 instead.

Does a 301 redirect hurt SEO?

No. Google has said 3xx redirects do not lose PageRank. Rankings drop only when redirects are set up badly.

Mistakes that do cost rankings

  • Redirect chains. A to B to C slows crawling. Point A straight at C.
  • Redirect loops. A to B and back to A. The page never loads.
  • Everything to the homepage. Google may treat these as soft 404s. Send each old page to its closest match.
  • Removing redirects too soon. Old links break and their value is lost.
  • Mixing Redirect and RewriteRule for the same URLs. They run in different Apache modules, so the result can surprise you.

How do I test a 301 redirect?

Run curl -I https://example.com/old-page. The first line should show 301, and the Location header should show the new URL.

$ curl -I https://example.com/old-page.html
HTTP/1.1 301 Moved Permanently
Location: https://example.com/new-page/

Browsers cache 301s hard. Test in a private window, or with curl, so an old cached redirect does not fool you.

How long should I keep a 301 redirect?

Keep it for at least a year. Keep it longer if other sites still link to the old URL.

Google needs to recrawl the old URL a few times to move everything over. Links from other websites keep sending visitors to the old address for years, and the redirect is what keeps those visitors and that link value.

Generate your redirects for free

Paste a list of old and new URLs into the 301 redirect generator. It writes the .htaccess, PHP and Nginx versions at once, runs in your browser, and needs no sign up. For other site checks, see the free SEO tools.