HTACCESS Redirect Generator
Generate .htaccess redirect rules.
Runs in your browser Your input never leaves your device.
Report a problem with this tool
About this tool
Generate Apache .htaccess lines that redirect one path to another URL. Enter the old path, the full destination URL and choose 301 (permanent) or 302 (temporary). The output gives a simple Redirect directive, plus an equivalent mod_rewrite block that is commented out, so you can paste everything without applying two rules: remove the Redirect line and the leading # characters if you prefer mod_rewrite. Regex characters in the path are escaped for you in the rewrite pattern. The text is generated in your browser; copy it into your .htaccess file. It only works on Apache servers (or compatible ones) with .htaccess enabled.
How to use it
- Enter the old path, starting with a slash.
- Enter the full destination URL.
- Choose 301 for a permanent move or 302 for a temporary one.
- Copy one of the two options into your .htaccess file.
Example
From /old-page, To https://example.com/new-page, Type 301
# Apache .htaccess Redirect 301 /old-page https://example.com/new-page # Alternative with mod_rewrite (use this OR the Redirect line above, not both): # RewriteEngine On # RewriteRule ^old-page$ https://example.com/new-page [R=301,L]
Force HTTPS
Sends every http:// request to the same URL on https://. Put it near the top of the .htaccess file in your site root.
If your site is behind a load balancer or CDN that handles HTTPS for you, %{HTTPS} is always off and this rule loops. Use RewriteCond %{HTTP:X-Forwarded-Proto} !https instead.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] Redirect www to non-www
Removes the www. prefix for any domain and keeps the path and query string.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301] Move a whole domain
Keeps the path, so old-domain.com/blog/post goes to new-domain.com/blog/post. Put it in the old domain's .htaccess and replace both names.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?old-domain\.com$ [NC]
RewriteRule ^(.*)$ https://new-domain.com/$1 [L,R=301] Redirect or RewriteRule? 301 or 302?
Redirect (mod_alias) is the simplest way to send one path to one URL. Use RewriteRule (mod_rewrite) when you need conditions, such as the host name or HTTPS, or patterns. Avoid using both for the same URLs: the two modules run separately and their rules can conflict.
Use 301 when the move is permanent, so search engines pass the old URL's ranking to the new one. Use 302 while testing: browsers cache 301 redirects, which makes a wrong rule hard to undo.
Common problems
- Redirect does nothing
- The .htaccess file may be disabled (AllowOverride) or your server may not be Apache. Nginx and others need their own configuration.
- I want the RewriteRule instead
- Delete the Redirect line and remove the # at the start of the three mod_rewrite lines.
- Special characters in the path
- In the mod_rewrite lines regex characters are escaped. Spaces and query strings are not handled, so quote or adjust those by hand.