Redirecting pages with htaccess

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

1195 views

Edited: 2017-11-29 01:21

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.

Tell us what you think:

  1. This tutorial shows how to redirect www to non-www, and non-www to www, using htaccess.

More in: htaccess