.htaccess redirect generator
Enter your redirects, choose the options and copy the generated configuration.
After deploying, test your redirects with the Redirect checker or the Bulk checker.
Want to understand the rules? Read the .htaccess redirect guide.
How the .htaccess redirect generator works
Enter your domain and choose what should happen: redirect HTTP to HTTPS, pick a preferred host (don't change, without www or with www) and add single page redirects as old-path new-path-or-URL, one per line. Old paths start with a slash, the target can be a path like /new-page or a full URL on another domain. The status code you select (301, 302, 307 or 308) applies to these page redirects. The output updates as you type.
For example.com with HTTPS, without www and one page redirect, the result looks like this:
# Generated with https://redirect-tester.com/htaccess-redirect-generator/
RewriteEngine On
# HTTPS and example.com as preferred host in one redirect
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]
# Page redirects (301)
Redirect 301 /old-page /new-page
What the rules do
- HTTPS and www in one hop: the
RewriteCondlines are combined with[OR], so a request that is on HTTP or on the wrong host is sent straight to the final address.http://www.example.com/goes directly tohttps://example.com/instead of taking two redirects. That avoids unnecessary redirect chains. [R=301,L]makes it a permanent redirect and stops processing further rewrite rules.Redirect 301 /old /newcomes from mod_alias and is the simplest way to move a single page. Note that it matches by prefix:/old/subis redirected to/new/subas well.
Use 301 or 308 for moves that are meant to last, and 302 or 307 only for temporary ones. The differences are explained in 301 vs. 302 and 307 vs. 308.
Where to put the .htaccess file
Place the rules in the .htaccess file in your document root, the folder that contains your index.php or index.html. If the file already exists, for example from WordPress, add the generated block above the existing rules so the redirects are applied first. Keep a backup of the original file.
Two requirements have to be met:
- mod_rewrite must be enabled. On most shared hosting it is. On your own server, enable it with
a2enmod rewrite(Debian/Ubuntu) and restart Apache. AllowOverridemust allow the directives, otherwise Apache ignores the.htaccessfile. See the Apache .htaccess documentation.
If your site runs behind a CDN or load balancer that terminates TLS, %{HTTPS} is always off on your server and the HTTPS rule causes a redirect loop. In that case, set up the HTTPS redirect at the CDN instead.
Test after deploying
Once the file is live, check the HTTP version, the www version and a few old page URLs with the redirect checker. Every URL should reach its target in a single hop with the status code you expect. More background and examples are in the .htaccess redirect guide.
Frequently asked questions
- Why is the HTTPS and www redirect always a 301?
Switching to HTTPS and choosing a preferred host are permanent decisions, so the generator always uses a permanent
301for them. The status code you select only applies to the single page redirects. If you prefer308, you can changeR=301toR=308in the output.- I get a 500 Internal Server Error after uploading the .htaccess. What now?
A 500 error usually means Apache cannot process a directive, most often because mod_rewrite is not enabled or the file contains a typo. Check the Apache error log, which names the problematic line. Also make sure the file was saved as plain text with Unix line endings and that the name is exactly
.htaccess.- Can I use the generator for WordPress?
Yes. Add the generated rules above the
# BEGIN WordPressblock, because WordPress may rewrite that block when you save permalinks. Also check that the WordPress and site address in the settings match your preferred host and HTTPS, otherwise WordPress and Apache may redirect back and forth. More in the WordPress redirect guide.
More free tools
-
Bulk checker
Check up to 20 URLs at once, e.g. your redirect map after a domain migration. Export as CSV.
-
Header checker
See all HTTP response headers of every hop, including Location, Cache-Control and HSTS.
-
nginx redirect generator
Generate nginx server blocks and return rules for your redirects.
-
HTTP status codes
All HTTP status codes explained, with notes on how Google treats them.
-
JSON API
Check redirects from your scripts and CI pipelines with a simple JSON API.