By using this site you agree to the use of cookies.

Learn more

Share via:

Redirecting pages with htaccess

How to redirect pages with htaccess. This tutorial shows how to do redirects in htaccess.

Edited: 2017-06-18 16:14

This tutorial shows how to Redirect pages with htaccess, in this turotial we deal with two types of redirects, Temporary and permanent redirects. When performing redirects in htaccess, you can either use Redirect, RedirectMatch, and mod_rewrite.

Redirecting old URLs for content that has been moved, can be an important step to ensure that users will not end up confused or frustrated because they can no longer find the content they are looking for. It is also useful to prevent loss of visitors.

Redirect

Used when a page is moved permanently, often used when changing domains, or changing the site structure.

Redirect 301 /old-page.html http://www.example.com/newpage.html

Redirecting the whole Site:

Redirect 301 / http://www.example.com/

Note. When wanting to use the Temporary Redirect, simply change 301 to 302.

Redirect 302 / http://www.example.com/

Using RedirectMatch

If you have changed the file extension of a page, for example to .php from .html, you can use a regular expression to remember the file name, and redirect to the new file extension.

RedirectMatch 301 (.*)\.html$ http://www.example.com$1.php

Note. The content of the parentheses is remembered, or back-referenced. The . dot allows any character, and the * asterix allows none or more characters to be present.