Skip to content
PithLab

↑↓ to move · Enter to open · Esc to close

Htaccess Redirect Generator (Apache and Nginx)

Write Apache and Nginx 301/302 redirect rules without regex mistakes.

Use via API

Generate Apache .htaccess and Nginx redirect rules: single page, bulk list, domain move, force HTTPS, add or remove www, add or remove trailing slashes. Send JSON, get JSON back.

POST https://pithlab.com/api/v1/tools/htaccess-redirect-generator

curl https://pithlab.com/api/v1/tools/htaccess-redirect-generator \
  -H "Authorization: Bearer $PITHLAB_KEY" \
  -H "Content-Type: application/json" \
  -d '{"mode":"bulk","pairs":"/old-page.html /new-page\n/blog/2019/hello /blog/hello"}'

Fields and responseOpenAPI documentGet an API key

About the Htaccess Redirect Generator

Generate 301 and 302 redirects for .htaccess and Nginx: single pages, bulk lists, domain moves, HTTP to HTTPS, www and trailing slashes, safely escaped.

How to use the Htaccess Redirect Generator

  1. Choose the kind of redirect: one page, a bulk list, a whole domain, HTTP to HTTPS, adding or removing www, or trailing slashes.
  2. Pick 301 for a permanent move or 302 for a temporary one, then fill in the paths or domains.
  3. For bulk redirects, paste one old path and new URL per line, separated by a space.
  4. Copy the Apache rules into .htaccess or your virtual host, or copy the Nginx tab into your server block, then test with a private window.

Examples

Redirect one page (mod_rewrite)

Input

301, /old-page.html to /new-page

Output

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

Input

301, /old-page.html to /new-page

Output

# Inside your server { } block
location = /old-page.html {
    return 301 /new-page$is_args$args;
}
Move a whole domain

Input

301, old-site.com to new-site.com

Output

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?old-site\.com$ [NC]
RewriteRule ^ https://new-site.com%{REQUEST_URI} [R=301,L]
Force HTTPS

Input

301, HTTP to HTTPS

Output

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Frequently asked questions

Should I use a 301 or a 302 redirect?

Use 301 when a page has moved for good. Search engines transfer the old URL's ranking to the new one and browsers cache it, sometimes for a long time. Use 302 while testing or for temporary moves, then switch to 301 once you are sure.

Why does the pattern have dots escaped, like old-page\.html?

RewriteRule and RedirectMatch patterns are regular expressions, where a dot means any character and ?, +, ( and ) have special meanings. The generator escapes them so the rule matches only the exact path you typed, and anchors it with ^ and $ so /old-page.html does not also catch /old-page.html-backup.

Can I redirect a URL with a query string?

Yes, with mod_rewrite. Paths like /products?id=12 become a RewriteCond on %{QUERY_STRING} plus a rule with the QSD flag so the old query is dropped. mod_alias (Redirect and RedirectMatch) cannot see query strings. In Nginx the rule compares $request_uri.

Why are spaces and quotes rejected?

A space or line break would split a directive or add a new one, which can break your server config or inject rules. Write spaces as %20 and other special characters percent-encoded, as they appear in a real URL. Non-ASCII letters such as é are encoded for you.

Where do the Apache rules go?

In the .htaccess file in your site's root, or inside the <VirtualHost> block, which is faster. Patterns start with ^/? so they work in both places. mod_rewrite must be enabled, and .htaccess needs AllowOverride FileInfo. Nothing you enter here is uploaded.