Redirects with .htaccess: copy-ready examples

Working .htaccess redirect rules for single pages, whole folders, domain changes, HTTPS and www — with notes on 301 vs 302 and avoiding loops.

Guide2 min readLast updated
On this page
  1. One page to another
  2. A whole folder, keeping the rest of the path
  3. Old domain to new domain
  4. Force HTTPS
  5. HTTPS and non-www in one step
  6. HTTPS and www in one step
  7. Remove .html from URLs
  8. Avoiding problems
  9. Common questions

Put redirect rules near the top of public_html/.htaccess, above any # BEGIN WordPress block. Test each in a private window, since browsers cache 301s.

One page to another

Redirect 301 /old-page.html https://yourdomain.co.uk/new-page/

A whole folder, keeping the rest of the path

RedirectMatch 301 ^/blog/(.*)$ https://yourdomain.co.uk/news/$1

Old domain to new domain

In the old domain’s .htaccess (on hosting that still serves it):

RewriteEngine On
RewriteRule ^(.*)$ https://newdomain.co.uk/$1 [L,R=301]

Force HTTPS

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

HTTPS and non-www in one step

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://yourdomain.co.uk%{REQUEST_URI} [L,R=301]

HTTPS and www in one step

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.yourdomain.co.uk%{REQUEST_URI} [L,R=301]

Remove .html from URLs

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]

Avoiding problems

Prefer to follow along?

New to Traxio? The PHP hosting page lists the PHP versions, database and tools every account gets.

Common questions

How do I redirect one URL to another?

For a whole domain or folder, use Account Manager › Site Redirection in DirectAdmin. For individual pages, add redirect rules to your .htaccess file. Use a 301 for anything permanent so search engines transfer the ranking to the new address.

What’s the difference between a 301 and a 302 redirect?

A 301 says the move is permanent and tells search engines to transfer the ranking to the new address; a 302 says it is temporary and they should keep the old one indexed. Use 301 for anything you do not intend to undo.

This is a general web-hosting explanation, not specific to Traxio.

The practical risk is using a 302 for a permanent move: the old URL keeps its ranking, the new one never builds any, and it can take months to notice.

Browsers cache 301s aggressively, so test a new redirect in a private window — otherwise you may keep seeing the first version you tried.

Spotted something out of date or unclear? Tell us and we’ll fix it.

Keep going