HTACCESS Redirect Reference Guide

Outline

Below is a reference for commonly used htaccess redirects for an Apache server. This compilation was built to consolidate common rules I use in my day-to-day work for easy reference, hopefully, this will help you as well! If you think there are more rules that I should add, feel free to note them in the comment section.

Tip: Try out your redirect rules before going live using Made With Love's htaccess testing tool.

Force HTTPS

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

Redirect WWW to non-WWW

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.com
RewriteRule (.*) https://domain.com/$1 [R=301,L]

Redirect non-WWW to WWW

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com
RewriteRule (.*) https://www.domain.com/$1 [R=301,L]

Tip: You only need to add the RewriteEngine On line once in your htaccess file

Redirect a single page

To URL inside domain

Redirect 301 /a/path/on/your/site/ /a/different/path/on/your/site/

To URL outside domain

Redirect 301 /a/path/on/your/site/ https://www.domain.com/some/path/

Redirect whole domain

Redirect 301 / https://domain.com/

Redirect using regex

RedirectMatch 301 "<your path with regex>" "https://domain.com/some/path/"

I hope you found this reference useful and will help in your day-to-day Apache server work implementing htaccess redirects.

Comments

Login to Add comments.