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.

ReferenceIntermediate2 min readUpdated

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

New to Traxio? The PHP hosting page lists the PHP versions, database and tools every account gets, free for the first 30 days.

Popular

Tip: press / to search from any pageSee all results