Skip to main content
ilovecalcs logoilovecalcs.

Dev · Live

.htaccess Redirect Generator, clean Apache rules in seconds.

Toggle options to force HTTPS, manage www redirects, add custom 301 redirects, and configure a 404 error page. Copy production-ready Apache directives instantly — nothing leaves your browser.

.htaccess guideApache 2.4

Configure redirect rules

Toggle options — the output updates instantly.

www Redirect

Enforce the canonical domain variant for your site.

.htaccess
8 lines
Apache 2.4 · mod_rewrite · 1 active rule

Deployment guide

  1. 1Configure your rules in the options panel.
  2. 2Click Copy Code above and paste into a file named .htaccess.
  3. 3Upload to your web root — usually public_html/ on shared hosts.
  4. 4Test with curl -sI http://yourdomain.com and confirm the 301 status before going live.

.htaccess guide

What is .htaccess, why it matters, and how redirects affect SEO.

What is an .htaccess file?

.htaccess (Hypertext Access) is a per-directory configuration file for Apache HTTP Server. When Apache receives a request for a file inside a directory, it first checks whether an .htaccess file exists there and, if so, applies the directives it finds before serving the response.

Unlike Apache's main server configuration (httpd.conf), which requires a server restart after every change, .htaccessis read on every request. This makes it ideal for shared hosting environments where users cannot modify the global server config. The trade-off is a minor performance cost: Apache must stat the directory tree on every request to check for the file.

Why 301 redirects are critical for SEO

A 301 Moved Permanently redirect tells search engine crawlers that a URL has permanently moved to a new location. When Google encounters a 301, it transfers roughly 99% of the original page's ranking signals (sometimes called "link equity" or "PageRank") to the destination URL. This is fundamentally different from a 302 Found (temporary) redirect, which tells crawlers the move is provisional and they should keep the original URL indexed.

Common scenarios that require 301 redirects for SEO health:

  • Migrating from HTTP to HTTPS: Every HTTP URL (http://) must 301-redirect to its HTTPS equivalent. Without this, search engines may index both versions, splitting link equity and causing duplicate-content penalties.
  • Canonicalising www vs non-www: http://example.comand http://www.example.com are technically separate URLs. Choose one canonical form and 301-redirect the other. Failing to do so means incoming links are split between two URLs.
  • Restructuring URLs: When you rename a page, delete a category, or move content to a new path, a 301 ensures search engines update their index and users with old bookmarks are not stranded.

HTTP to HTTPS migration

The simplest correct way to redirect all HTTP traffic to HTTPS on Apache uses mod_rewrite:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTPS} off checks that the request did not arrive over TLS. %{HTTP_HOST} preserves the original hostname and %{REQUEST_URI} preserves the path and query string, so visitors are redirected to the exact HTTPS equivalent of whatever URL they requested.

If you are behind a load balancer or reverse proxy that terminates TLS before passing requests to Apache, %{HTTPS} may always appear as off. In that case, use the forwarded-protocol header instead: RewriteCond %{HTTP:X-Forwarded-Proto} !https.

www vs non-www: which should you pick?

Neither is inherently better for SEO — what matters is consistency. Pick one, 301-redirect the other permanently, and set the chosen form as the canonical URL in your sitemap and <link rel="canonical">tags. Changing your preference later is possible but requires time for crawlers to fully re-index.

Technical consideration: the non-www form is slightly simpler (fewer DNS records, no CNAME for the root domain limitation). The www form makes it easier to point subdomains at CDNs or load balancers, and browsers will not send cookies set onwww.example.com to api.example.com, which can be a useful isolation property.

mod_rewrite vs mod_alias

Apache provides two modules for handling redirects:

  • mod_alias — The Redirect andRedirectMatch directives. Simple, fast, and suitable for straightforward path-to-path redirects. TheRedirect 301 /old /new directive generated by this tool uses mod_alias.
  • mod_rewrite — The RewriteRule andRewriteCond directives. Regex-based and much more powerful: can inspect request headers, server variables, and chain multiple conditions. Used for HTTPS enforcement and www canonicalisation because these depend on server variables (%{HTTPS},%{HTTP_HOST}) that mod_alias cannot access.

Both modules must be enabled in your Apache installation. On Ubuntu/Debian servers: sudo a2enmod rewrite. Most shared hosting providers enable both by default.

Order of directives

In Apache, RewriteRule directives are evaluated top-to-bottom. When multiple rules match a single request, they are applied sequentially unless the [L] (last) flag stops processing. This tool always outputs HTTPS enforcement before www redirect rules — this is the correct order, because a www redirect that generates an HTTP URL would be immediately caught by the HTTPS rule and redirected again (a two-hop redirect chain). Modern browsers handle this gracefully, but it is inefficient. For a one-hop solution, combine the conditions into a single rule for your production server.

Testing your redirects

Never deploy .htaccess changes to production without testing. Usecurl to inspect response headers without following redirects:

curl -sI http://example.com

Look for HTTP/1.1 301 Moved Permanently and aLocation: header pointing to the correct destination. Tools like redirect-checker.org and browser developer tools (Network tab → check for 301 status) are also useful.