User Tools

Site Tools


.htaccess redirects

Requirements: This requires the mod_rewrite module in apache. This is enabled by default on all KnownHost systems.

.htaccess files can be used in a website's document root directory (ex. /home/user/public_html/) for redirecting web site visitors from one document within your web site to another. .htaccess rules are recursive meaning that rules written in /home/user/public_html/.htaccess will apply to /home/user/public_html and /home/user/public_html/test/directory unless the specific rule is canceled out by a rule in a .htaccess file in a directory below it.

There are different types of redirects that can be performed via .htaccess files. Let's break them down and understand each type before learning how to use them.

301 (permanent) redirect

This is the most common type and typically what you'll want to use. While the difference between 301 and 302 is small, it is notable. 301 (permenant) indicates that the old link url (redirected URL) is no longer in use. Whereas the 302 redirect suggests that the old URL is temporarily offline, returning shortly.

302 (temporary) redirect

As previously stated, the 302 redirect is a temporary redirect solution. It's also apache's default redirect rule. So, when determining your method/option, you'll need to know if it's a permenant redirect or temporary.

For all documentation/article purposes, we will be using 301 redirects.

Creating the .htaccess file and uploading it to your server

Creating the .htaccess file, to use for 301 or 302 redirects is simple. You'll simply need to open your favorite text/document editor and create a new blank file. Add the relevant code/content to the document and save the file as "htaccessrules.txt". After you've confirmed the accurate rules inside the document, use your favorite FTP1) Client and upload the document to your website's document root (public_html). Once you've uploaded the document, simply rename it .htaccess

Via SSH or the terminal, this is easily done using your favorite text editor (eg. nano or vim) by creating a new file locally.

Redirect scenarios and code examples

Here are some examples of some common things .htaccess rewrite or redirect rules are used for.

Redirect a single page URL (html/php file) to another

Redirect 301 /retiredpage.html

Redirect a folder/directory URL (including all sub contents) to another

RedirectMatch 301 ^/oldname/

Redirect an entire domain name to another

RedirectMatch 301 ^(.*)$

Redirect .html files to .php files

RedirectMatch 301 (.*)\.html$$1.php

Redirect www to non-www (remove www)

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.knownhosttest\.com$ [NC]
RewriteRule ^(.*)$$1 [L,R=301]

Redirect non-www to www (force www)

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.knownhosttest\.com$ [NC] 
RewriteRule ^(.*)$$1 [L,R=301]

Redirect http to https (force https)

Here are two separate examples of how to do this. We can do this with Redirect in an If block:

<If "%{HTTPS} == 'off'">
  Redirect permanent "/" "https://domain.tld"

This can also be done using Rewrite rules. It might be better to avoid that complexity, but if there are already Rewrite rules in the .htaccess file, it might be necessary to use Rewrite rules anyway:2)

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R=permanent,L]

Check out this great tool for genenrating .htaccess files: .htaccess Generator Tool.
Official Apache .htaccess redirect documentation can be found here: Apache Rewrite Guide.

example derived from here.
developmental/how-can-i-use-htaccess-for-redirects.txt · Last modified: 2017/12/14 21:13 by Marjorie S.