Configure a redirect

3 min read07 November 2026

A redirect sends the browser from one URL to another. Used well, it unifies the canonical site name (www or no www) and preserves search rankings. Used badly, it creates loops, breaks forms or hides an error for months.

1. Choose the HTTP code

Code Meaning Use
301 moved permanently canonicalization, stable migration
302 found, temporary testing, short maintenance
307 temporary, method preserved APIs, forms
308 permanent, method preserved API migration

A 301 is cached by browsers and search engines. Do not use it during a test: a bad 301 can stay active for visitors a long time. For a switch you need to verify, start with 302.

2. Use the panel option

If KeyHelp offers a per-domain or per-path redirect, prefer it: it applies before your files and avoids .htaccess mistakes. Enter the source URL, target URL and code. Check that the target includes the right scheme (https://) and host name.

For a global www to non-www redirect, the panel option is often enough. Otherwise, move to Apache configuration.

3. Write the Apache rule

In .htaccess, at the site root:

Text
RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]

%1 reuses the condition capture, $1 the rule capture. The rule keeps the requested path: /contact stays /contact, without going through the home page.

To force HTTPS and then strip www, chain two rules instead of merging them:

Text
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]

The first rule switches to HTTPS while keeping the requested name; the second only applies once HTTPS is active, then removes www. That separation prevents a rule from applying again to its own target.

4. Avoid loops

A loop happens when the rule also applies to its target. Always check:

  • the target does not match the condition;
  • the detected scheme matches reality (TLS proxy);
  • no other rule redirects in the opposite direction;
  • the subdomain and the main domain do not bounce to each other.

Do not redirect a POST to a GET-only page unintentionally: use 307/308 when the method must be preserved.

5. Verify

Bash
curl -I http://www.your-domain.tld/contact
curl -IL http://your-domain.tld/contact

The first command should return 301 with a Location of https://your-domain.tld/contact. The second follows the chain: one redirect is enough, not three. Also test a nonexistent page and a form.

Finish with a check in a private browser window, then remove test rules. Keep a note of the active rule: in six months, nobody will remember why it exists.

Advertisement

Was this article helpful?

Read next