.htaccess redirect generator

This .htaccess redirect generator creates code for your .htaccess file: a redirect to HTTPS and your site's main address, permanent and temporary page redirects, a move between domains, and a 404 error page.

The rules are created in the browser and need JavaScript. A fixed example is shown.

Site address
Every http request is redirected to the same address over https. The rule also detects requests that arrive through a proxy or CDN that already uses HTTPS, to avoid a redirect loop.
Main site address
The site addresses with and without www are redirected to one address, so that search engines index one address for each page.
The domain of the site, for example example.com. Enter an internationalized domain name in its Punycode form, which starts with xn--.
Page redirect
A path from the site root that starts with a slash (/), for example /old-page. A path with non-ASCII characters, such as /café, can be entered as is or percent-encoded.
A path on the site, for example /new-page, or a full address that starts with https://. Enter a space as %20.
Redirect type
A permanent redirect (301) passes search rankings to the new address. A temporary redirect (302) suits a change for a limited time.
Each line holds an old path and a new address, separated by a space, for example /old-page /new-page. Every line gets a permanent redirect (301). Lines that start with a hash sign (#) are skipped.
Advanced settings
Optional. Every address on the old domain, with and without www, is redirected to the new domain with the same path. The redirect goes to an https address.
Trailing slash
An address with a trailing slash and one without it are redirected to one form. Adding the slash skips existing files and addresses with an extension, and removing it skips existing folders.
Addresses without an extension
An address with the extension is redirected to the address without it, and the server shows the original file. This suits a site built from separate files and is not needed when a content management system sets the addresses.
Optional. The path of a page on the site, for example /404.html, shown when the requested page does not exist.
Add the code at the top of the .htaccess file in the root folder of the site, before its existing rules.
Page redirects: 1. Lines of code: 12.

Everything runs in your browser; nothing is sent or stored.

  • The rules work with the Apache and LiteSpeed web servers. In the "Site address" section, choose whether to redirect to HTTPS and whether the main address is with or without www, and enter the domain.
  • When both the HTTPS redirect and a main address are chosen, every rule redirects straight to the final address. This way, each request is redirected only once, not through a chain of redirects.
  • In the "Page redirect" section, enter the old path and the new address, and choose a permanent (301) or temporary (302) redirect. To redirect several pages, enter an old path and a new address on each line of the "Bulk redirects" field.
  • The server compares the requested path after decoding the percent-encoding. So for a path with non-ASCII characters, the rule holds the characters themselves, and the file must be saved as UTF-8. In the new address, the server encodes these characters itself when it redirects.
  • In the "Advanced settings" section, you can set up a move to a new domain, a trailing slash, addresses without an extension, and a 404 error page.
  • Copy the code by clicking "Copy", and add it at the top of the .htaccess file in the root folder of your site. Before editing the file, save a backup copy of it. Browsers cache permanent (301) redirects, so test your redirects in a private window.

What is the .htaccess file?

The .htaccess file is a configuration file that changes how requests are handled in the folder it is in and in the folders below it, without access to the main server configuration. The rules this generator creates work with the Apache and LiteSpeed web servers. The redirect rules are based on the mod_rewrite module: a RewriteCond line sets a condition, and the RewriteRule line after it sets the pattern of the path being checked and the address the request is redirected to.

Permanent or temporary redirect

A permanent redirect (301) indicates that the page has moved to a new address for good, and search engines pass the ranking of the old address on to the new one. A temporary redirect (302) suits a change for a limited time, such as a seasonal sale page or a maintenance page, when the original address will be used again. When you move to a new site or a new domain, redirect each old page to the matching page on the new site, not to the home page, to keep the ranking of each page.

Rule order in the file

The server checks the rules in the file in order, and the first matching rule determines the redirect. The generator orders the rules so that page redirects come before the redirect to HTTPS and the main address. When the HTTPS redirect is selected, each page redirect leads straight to the full address of the new page, so a visitor who opens an old address reaches the new page in one redirect, without a chain of redirects that slows down loading. Because of this order, add the code before any existing rules in the file, such as the rules of your content management system that pass every request to a single file.

Parts of the generated code

  • R=301A permanent redirect. In a temporary redirect, R=302 appears instead.
  • LStops processing the rules after this rule runs.
  • NCA case-insensitive match, for example in the domain name.
  • NEThe new address is sent as is, without encoding the % character again. The flag is added when the new address contains percent-encoding, and also in the rule for addresses without an extension.
  • %{HTTP_HOST}The domain name the request was sent to.
  • %{REQUEST_URI}The requested path, which is appended to the new address in the redirect to HTTPS and the main address.
  • %{HTTP:X-Forwarded-Proto}The header in which a proxy or CDN states the protocol of the original request. The condition on this header prevents a redirect loop.
  • ErrorDocument 404 /404.htmlThe page shown when the requested page does not exist.
Did you know?

With Linux hosting, you can edit .htaccess files.

Linux hosting plans

.htaccess redirect generator FAQ

Where do I find the .htaccess file, and how do I create one?

The .htaccess file is in the root folder of your site, where the home page is. The file name starts with a dot, so some file managers and FTP programs hide it until you turn on the option to show hidden files. If the file does not exist, you can create a new file named .htaccess and paste the code into it.

Does a page redirect also apply to the path with a trailing slash?

No. The rule for each page matches the path exactly as you entered it, so /old-page and /old-page/ are two different paths. To redirect both paths, enter a separate line for each path in the "Bulk redirects" field. Uppercase and lowercase letters in the path are also treated as different.

Can I redirect an address with parameters after a question mark?

No. The old path cannot contain a question mark (?) or a hash sign (#), because the rule checks only the path of the address, without the parameters. Redirecting by parameters requires a condition on the %{QUERY_STRING} variable, and the generator does not create conditions on parameters.

Can I redirect pages with accented or non-Latin characters in the address?

Yes. You can enter a path with non-ASCII characters, such as /café, as is or percent-encoded, and in both cases the rule is created with the characters themselves. Save the .htaccess file as UTF-8, or the rule will not match the path. In the domain fields, enter an internationalized domain name in its Punycode form, which starts with xn--.

Why is my redirect not working after I save the file?

Browsers cache permanent (301) redirects, so after changing a redirect, test it in a private window. If the redirect does not work in a private window either, make sure the code is placed before the existing rules in the file and that the old path was entered exactly as it appears in the address.

Is there a limit on the number of redirects?

No. The number of redirects is not limited, but the "Bulk redirects" field accepts up to 100,000 characters. Every line in the field gets a permanent redirect (301). Invalid lines, lines with an old path that was already entered, and lines whose new address is the same as the old path are left out of the code and listed in a message below the field.

Shopping cart

Your cart is emptyYou can start with one of our popular plans.

Site search

Accessibility

Text size
100%
Accessibility statement