htaccess Redirect

How to redirect webpages

The .htaccess file is a small text document that generally sits in the same location as your index.php or index.htm pages. It gives you the ability to interact with Apache on an individual domain-to-domain and directory-to-directory basis.

You can place the htaccess file anywhere where you'd like to control the flow of visitors. So for instance you can protect directories and redirect the traffic visiting those pages. This page will show you how to use the .htaccess file to redirect your visitors in different ways. Note that this is a powerful system file, if the syntax is incorrect in anyway, it could render your site unusable. Always take a backup.

Check out the 301 redirect tutorial at for more information on creating search engine friendly redirects

Redirect to another page

To redirect one page to another page:

Redirect /old-index.html

Redirect to another website

To redirect an entire website from one url to another:

Redirect 301 /

Redirect index to subdirectory

To redirect a page to a subdirectory:

Redirect /index.html

Redirect filepaths

To redirect a filepath to another filepath:

Redirect /foldername/filename.html

To change file extention

If you've changed your pages from .html to .php:

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

Specifying folder default page

To change the default webpage loaded by the server:

DirectoryIndex index.php

Redirect www to non-www

To redirect to

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.mynewwebsite\.com$ [NC]
RewriteRule ^(.*)$$1 [L,R=301]
Start Learning For Free - Learn Web Design, Coding, Mobile App Development & More

Redirect non-www to www

To redirect to

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

Give 307 'Site Under Maintenance' Header on all webpage requests

To return a 307 Site Under Maintenance header to those visiting the site. Create a temporary file called 307.php and inside it place the message to give to your visitors and upload it to your public root directory.

Next create a totally separate .htaccess file called .htaccess.307 with the following text:

RewriteEngine On
RewriteBase /
# Before using this htaccess, you have to change this digits to match your
# own IP address: This will keep you with access to the site. as long as
# your IP doesn't change:

RewriteCond %{REMOTE_ADDR} !^123\.123\.123\.123$

# The last 2 lines take the site offline. the basically say if: page request is
# NOT 307.php, show 307.php but only once. Stops it from looping endlessly.
RewriteCond %{REQUEST_URI} !^/307\.php$

# The browser gets this bit, so you need the full website address.
RewriteRule ^(.*)$ [R=307,L]

The above includes comments for your convenience. To get the redirect up and running, rename your existing .htaccess file to live.htaccess then rename this one to .htaccess

Alternative Redirect 1: Meta Refresh

It's also possible to redirect traffic when visiting a page with the following line inside the head tag.

To do an immediate redirect:

<meta http-equiv="refresh" content="0; url=" />

To redirect after 5 seconds:

<meta http-equiv="refresh" content="5; url=" />

Alternative Redirect 2: PHP Header Redirect

You're able to redirect traffic by putting the following line at the very top of a php document (nothing can be above it).

            header ('HTTP/1.1 301 Moved Permanently');
            header( "" );
Copyright © 2014 - All rights reserved. By using this website you abide by the site's Terms and Conditions.
How To Enable Cookies