Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages

Free tool · Developer tools

Htaccess redirect generator

An .htaccess redirect tells an Apache server to send visitors and search engines from an old address to a new one: 301 for a permanent move, 302 for a temporary one. Paste old and new addresses, or choose a domain move or HTTPS and www, and copy rules that match only what you meant.

  • Free
  • No sign-up
  • Runs in your browser

By Maaz Zindani · Updated

What do you want to redirect?
Redirect type

The new address can be a path such as /new-page/ or a full URL. Old addresses with ?query strings are handled too.

Your .htaccess rules
# Page redirects (mod_alias): each matches one page, with or without a trailing slash RedirectMatch 301 "^/old-page\.html/?$" "/new-page/" RedirectMatch 301 "^/blog/2019/spring-sale/?$" "https://example.com/offers/" # Old addresses with a query string (mod_rewrite); QSD drops the old query string RewriteEngine On RewriteCond %{QUERY_STRING} ^id=42$ RewriteRule ^product\.php$ /shop/blue-mug/ [R=301,L,QSD]
  • 301 permanent
  • 3 redirects

Add the rules near the top of the .htaccess file in your site's top folder, keep a copy of the old file, then test a few old addresses in a private window. The download is named htaccess.txt: rename it to .htaccess.

How to use the Htaccess Redirect Generator

  1. Choose what to redirect: Pages (a list), A whole domain or HTTPS and www.
  2. Pick the Redirect type: 301 for a permanent move, 302 for a temporary one, or 410 for pages removed for good.
  3. Fill in the paths or domains. For a list, type the old path, a space and the new address on each line.
  4. Read the notes under the rules, then Copy them, or Download and rename the file to .htaccess.

How do I redirect a page with .htaccess?

Add a redirect rule to the .htaccess file in the top folder of the site. For one page that has moved, the shortest rule uses Apache's mod_alias: Redirect permanent "/old-page.html" "/new-page/". The old address is a path starting with a slash; the new one can be a path on the same site or a full URL on another. Apache's documentation lists .htaccess among the places these rules may go, as long as the server allows FileInfo overrides there.

By default this tool writes the exact-match form instead: RedirectMatch 301 "^/old-page\.html/?$" "/new-page/". It redirects /old-page.html and /old-page.html/ and nothing else. The next section shows why that matters. The examples below are written by the tool for common jobs.

Common redirect jobs and the rules the tool writes for them
JobRule written
One page, exact (default)RedirectMatch 301 "^/old-page\.html/?$" "/new-page/"
A folder and everything in itRedirect permanent "/blog/2019/" "/archive/"
Temporary moveRedirectMatch 302 "^/summer-menu/?$" "/menu/"
Page removed for goodRedirectMatch 410 "^/discontinued-mug/?$"
Old address with a query stringRewriteCond %{QUERY_STRING} ^id=42$ RewriteRule ^product\.php$ /shop/blue-mug/ [R=301,L,QSD]

Why does a Redirect rule move pages I did not list?

Because the plain Redirect directive matches the start of the path. Apache's documentation says any request beginning with the old path is redirected, and the rest of the path is added to the new address. So a rule for /shop also moves /shop/mugs/ and /shop/cart, to the new address with /mugs/ and /cart added. Only whole path segments count, so /shopping is not caught.

That is useful for moving a whole folder, and it is how the tool writes rules when you untick Exact match only. For single pages it is a trap, and it can even loop: redirecting /shop to /shop/new/ would send /shop/new/ to /shop/new/new/ and on forever. The tool refuses that combination. With the exact option on, it uses RedirectMatch, which works with regular expressions instead of prefixes: the rule is anchored with ^ and $, dots are escaped, and an optional trailing slash is allowed.

Old addresses with a query string, such as /product.php?id=42, need different handling: Apache notes that mod_alias is for simple jobs and that the query string needs mod_rewrite. The tool writes RewriteCond %{QUERY_STRING} ^id=42$ followed by RewriteRule ^product\.php$ /shop/blue-mug/ [R=301,L,QSD]. The QSD flag drops the old query string, so the visitor does not land on /shop/blue-mug/?id=42.

Should I use a 301 or a 302 redirect?

Use 301 when the move is permanent and 302 when the old page will come back. Google's redirects guide explains the difference: with a permanent redirect, the indexing pipeline takes the redirect as a sign that the target should be the canonical page, while with a temporary one it does not, so the old address is more likely to stay in results. Google suggests choosing by how long you expect the redirect to stay in place.

A 410 is not a redirect at all: it tells browsers and crawlers that a page is gone on purpose, with no replacement. Use it for pages you removed and do not want to point anywhere. If a close replacement exists, a 301 to it is usually more helpful to visitors who follow an old link. If you leave the type out, Apache sends a 302, so the tool always writes the code explicitly.

How do I force HTTPS and remove www in one redirect?

Check both at once, so that every old form of the address reaches the right one in a single hop instead of two. For example.com without www, the tool writes these lines: RewriteEngine On / RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC,OR] / RewriteCond %{HTTPS} off / RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L,NE]. The first condition catches www.example.com, the OR joins it to the second condition, which catches any request over plain http, and the rule sends both to https://example.com with the same path.

The rule uses the HTTPS variable, which Apache documents as "on" for secure connections and "off" otherwise, and REQUEST_URI to keep the path. The query string is kept automatically. In an .htaccess file, mod_rewrite also needs RewriteEngine On, which the tool adds, and Apache asks for the FollowSymLinks or SymLinksIfOwnerMatch option to be enabled, which the server settings must allow for .htaccess files. Choose Add www for the opposite direction, or Leave as is to force HTTPS on every host name served from the folder.

How do I redirect a whole domain to a new one?

Put a host-name rule on the old domain's server. For the example, the tool writes RewriteCond %{HTTP_HOST} ^(www\.)?old-example\.com$ [NC] followed by RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L,NE]. Every address on the old domain, with or without www, goes to the same path on the new domain, so links to old articles still land on the right article rather than on the home page.

Keep the old domain registered and its server running for as long as people may follow old links, because the redirect only works while the old site answers. After any change, test a few old addresses in a private window and check that each arrives in one step at the right page, then use the HTTP header checker to see the status code and the Location header each one returns.

Use it with

Questions

How do I make a 301 redirect in .htaccess?

Add a rule such as RedirectMatch 301 "^/old-page\.html/?$" "/new-page/" to the .htaccess file in the site's top folder. The tool writes it from a list of old and new addresses.

What is the difference between Redirect and RedirectMatch?

Redirect matches the start of the path, so it also moves everything below the old path. RedirectMatch uses a regular expression, so an anchored rule matches one page only.

Should I use 301 or 302?

Use 301 for permanent moves and 302 for temporary ones. Google takes a permanent redirect as a sign the new page should be canonical; it does not do that for a temporary one.

How do I redirect http to https in .htaccess?

Use mod_rewrite: RewriteEngine On, RewriteCond %{HTTPS} off, then RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]. Choose HTTPS and www in the tool.

How do I redirect a URL with a query string?

mod_alias cannot match the query string, so the tool writes a RewriteCond on %{QUERY_STRING} and a RewriteRule with the QSD flag, which drops the old query.

What is a redirect chain?

One redirect leading to another. The tool warns when a new address in your list is itself redirected, and tells you the final address to point to instead.

The tool's results are computed in your browser by the code on this page, using the sources listed above; the explanations are checked against the tool's own output.