301 redirect generator

Choose what to redirect and enter your domain. You get rules for Apache's .htaccess and for an Nginx server block, ready to paste, with the commands to test them.

What to redirect
Web server
.htaccess
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Put the rules at the top of the .htaccess file in your site's root folder. Apache needs mod_rewrite and AllowOverride for that folder; most shared hosting has both.

Test it

shell
# .htaccess is read on every request: no reload needed
curl -I http://example.com/

The answer should be the status code you chose with a Location header pointing to the new address.

Nginx config needs a server where you have root. A small VPS runs Nginx, your site and its certificates. From €3.02 a month today, among 2,226 servers.

Works in your browser: nothing you type is sent anywhere.

How it is calculated

A redirect is an answer from the server with a 3xx status code and a Location header. The browser, or a search engine, then asks for the new address instead. The generator writes the redirect as early as possible: in Nginx a server block that does nothing else, in Apache rewrite rules at the top of .htaccess.

# Apache, .htaccess
RewriteRule ^old-page\.html$ /new-page [R=301,L]

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

Apache matches a regular expression, so dots and other special characters in a path are escaped, and the leading slash is left out because .htaccess removes it. Nginx compares an exact location as plain text. Both keep the visitor's query string unless the old address had one of its own. The HTTPS and www rules go to the final address in one step, rather than through two redirects in a row.

Example

Moving example.com to www.example.com: requests for http://example.com/blog, http://www.example.com/blog and https://example.com/blog all get one 301 to https://www.example.com/blog. Only https://www.example.com is served normally.

Which status code to use

CodeMeaningUse it for
301Moved permanentlyA page or a site that moved for good. Search engines pass the old address's ranking to the new one.A page or a site that moved for good. Search engines pass the old address's ranking to the new one.
302Found, temporarily elsewhereA short detour, such as a sale page or maintenance. The old address stays in search results.A short detour, such as a sale page or maintenance. The old address stays in search results.
307Temporary, same methodLike 302, but a form sent with POST is sent again with POST.Like 302, but a form sent with POST is sent again with POST.
308Permanent, same methodLike 301, but keeps POST. Useful for an API or a form endpoint that moved.Like 301, but keeps POST. Useful for an API or a form endpoint that moved.

Questions

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

A 301 says the address moved for good: browsers remember it and search engines replace the old address with the new one. A 302 says the move is temporary, so the old address keeps its place. Use 301 for a new domain, HTTPS or a renamed page; Google treats a 302 kept for a long time like a 301 anyway.

Should I redirect www to non-www or the other way round?

Either works for search engines as long as you pick one and redirect the other to it. The short form is easier to type; www makes it simpler to set cookies only for the site and to point the name at a CDN with a CNAME record.

Why does my redirect loop?

Usually because HTTPS ends at a proxy or CDN and the server sees every request as http://, so it redirects even the https:// ones. Tick the proxy option to test the X-Forwarded-Proto header instead. In Cloudflare, the Full (strict) SSL mode avoids the problem. Clear the browser cache after fixing it: browsers keep 301 redirects.

Does Nginx read .htaccess files?

No. Nginx has no per-folder configuration files, so rules from an .htaccess file have to be written into the server's configuration, as in the Nginx tab here, and Nginx reloaded. That needs access to the server, which a VPS gives you and shared hosting usually does not.