My Tool Studio
SEO

Htaccess Redirect Generator

This htaccess redirect generator writes Apache rewrite rules for the redirects most sites need: force HTTPS, add or remove www, strip trailing slashes, force lowercase URLs, remove .php or .html extensions, move a whole site to a new domain, and send old pages to new ones. Choose 301 or 302 for every rule, and override it line by line. Paste two columns of old and new URLs from a spreadsheet and each becomes an exact-match rule that points at a full address, so old URLs take a single hop. Optional blocks add a custom 404 page, blocked IP addresses, Gzip and browser caching. An Nginx tab writes the same rules for Nginx servers. Copy the code or download it as a file.

Always freeNo sign upRuns in your browser

Used for absolute redirect targets and the domain move

Used by every rule. Add 301 or 302 after a page redirect to override it.

Path to your error page, e.g. /404.html

One per line: subdomain then folder, e.g. blog /blog/. The subdomain must point at this server.

One per line or comma separated; ranges like 203.0.113.0/24 work

Every path on your domain is sent to the same path on the new one

Paste two columns from a spreadsheet, or type /old /new. Full URLs are reduced to their path and query.

2 page redirects. The download is named htaccess.txt; rename it to .htaccess on the server.

# Generated by MyToolStudio (Apache .htaccess)

<IfModule mod_rewrite.c>
RewriteEngine On

# Page redirects (listed first so each old URL takes one hop)
RewriteRule ^old-page/?$ https://example.com/new-page [R=301,L]
RewriteCond %{QUERY_STRING} ^id=12$
RewriteRule ^shop/?$ https://example.com/products/blue-mug [R=301,L,QSD]

# Force HTTPS and remove www in one hop
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]
</IfModule>

How to use

01

Set your domain and site-wide rules

Type your domain, pick 301 or 302 under Redirect type, and tick Force HTTPS, Remove www or Force www, Strip trailing slash and Force lowercase URLs as needed. Choose an extension to remove, and fill in a new domain only if the whole site is moving.

02

Paste page redirects and extras

Put one redirect per line: old URL, new URL, then 301 or 302 to override the default. Tabs, commas or spaces all work. Add a custom 404 path or IP addresses to block if you need them.

03

Copy the Apache or Nginx code

Keep the Apache .htaccess tab for Apache and LiteSpeed, or open Nginx for an Nginx server. Press Copy or Download, back up your current config, then add the new rules near the top.

Why Htaccess Redirect Generator

Common .htaccess redirect rules

These are the rules the generator writes, shown on their own so you can see what each part does. In .htaccess, the pattern in a RewriteRule is matched against the path without its leading slash, which is why a page rule starts with ^old-page and not ^/old-page. %1 in a target reuses the part captured by the last RewriteCond, and $1 reuses the part captured by the RewriteRule pattern itself. Swap example.com, old.com and new.com for your own domains.

GoalRewriteCondRewriteRule
HTTP to HTTPS%{HTTPS} off^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Remove www%{HTTP_HOST} ^www\.(.+)$ [NC]^ https://%1%{REQUEST_URI} [L,R=301]
Add www%{HTTP_HOST} !^www\. [NC]^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Strip trailing slash%{REQUEST_FILENAME} !-d^(.+)/$ /$1 [L,R=301]
Move to a new domain%{HTTP_HOST} ^(www\.)?old\.com$ [NC]^(.*)$ https://new.com/$1 [L,R=301]
One page to anotherNone needed^old-page/?$ https://example.com/new-page [R=301,L]

Mistakes that break .htaccess redirects

Most redirect problems come from a handful of habits. Check the list below before you upload a new file, and test a few old URLs with the Redirect Checker afterwards to confirm each one lands in a single hop.

  • Chaining rules so an old URL goes to HTTPS, then to www, then to the new page. Each extra hop slows the page and wastes crawl budget.
  • Sending every old page to the home page. Google tends to treat that like a missing page, so send each URL to its closest match.
  • Using 302 for a permanent move, which can keep the old URL in search results for longer.
  • Pasting new rules below a WordPress or CMS block that already rewrites every request, so your rules never run.
  • Editing the live file without a backup. One typo can return a 500 error for the whole site.
  • Forcing HTTPS behind a proxy that talks to your server over HTTP, which causes a redirect loop.

Common questions

What code does the htaccess redirect generator write?
RewriteCond and RewriteRule lines inside an IfModule mod_rewrite.c block, for page redirects, the domain move, HTTPS, www, lowercase, trailing slash and extension rules. The 404 option adds an ErrorDocument line, IP blocking adds a RequireAll block, and the Gzip and cache options add mod_deflate and mod_expires blocks. It avoids the older Redirect directive, because mixing it with RewriteRule makes rule order harder to predict.
Can I force HTTPS and remove www in the same .htaccess file?
Yes. When both are ticked, the tool writes one combined rule, so http://www.example.com goes straight to https://example.com in a single redirect. Force HTTPS with Force www works the same way.
How do I bulk redirect old URLs from a spreadsheet with .htaccess?
Copy the old and new URL columns from your sheet and paste them into the Page redirects box. Each line becomes an exact-match RewriteRule: full old URLs are reduced to their path, and a query string such as ?id=12 becomes a condition on the query. New URLs given as paths are turned into full URLs on your domain.
How do I redirect an old URL that has a query string in .htaccess?
Paste the full old URL, for example /shop?id=12, with its new address. The tool adds a RewriteCond that matches that exact query string, plus the QSD flag so the old query is not carried over to the new URL. QSD needs Apache 2.4 or newer.
Should I use a 301 or a 302 redirect in .htaccess?
Use 301 when the move is permanent, such as a new URL structure, a new domain or a switch to HTTPS. Use 302 for short-term moves, like a seasonal page that will come back. Search engines treat a 301 as a strong sign to index the new URL; with a 302 they may keep showing the old one.
How do I move my whole site to a new domain with .htaccess?
Enter your current domain in Your domain and the new one in the domain move field. The tool writes a rule that sends every path on the old domain, with or without www, to the same path on the new domain over HTTPS. Google recommends keeping such redirects in place for at least a year, so keep the old hosting and this file live.
Why do I get a redirect loop after forcing HTTPS?
This usually happens behind a proxy or CDN, such as Cloudflare in Flexible SSL mode, where visitors use HTTPS but the proxy talks to your server over HTTP. The HTTPS check then always sees off and redirects forever. Switch the CDN to Full SSL, or ask your host which header to test instead, such as X-Forwarded-Proto.
Can the htaccess redirect generator write Nginx rules?
Yes. Open the Nginx tab for server blocks that do the same job: return lines for HTTPS, www and domain moves, location or if lines for page redirects, and rewrite, try_files, error_page, deny, gzip and expires lines for the other options. Nginx cannot lowercase URLs without an extra module, so the tab says so instead. IIS uses web.config, which this tool does not write.
Where does the .htaccess file go on my server?
In the document root of your site, named exactly .htaccess with the leading dot and no extension. If a file already exists, back it up and merge the new rules in near the top, above any CMS block such as the WordPress one, instead of overwriting it.
How does the htaccess generator force lowercase URLs without RewriteMap?
RewriteMap only works in the main server config, not in .htaccess. So the tool writes the common workaround: if the path has a capital letter, 26 small rules swap each capital for its lowercase letter, then one 301 sends the visitor to the lowercase address. Paths that are already lowercase skip the whole block, so it adds no cost to normal requests.

More SEO tools

View all