HTACCESS REDIRECT HOW TO REDIRECT WEBPAGES

Sep 17, 2015

By htaccessredirect.co.uk

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 webconfs.com for more information on creating search engine friendly redirects

 

REDIRECT TO ANOTHER PAGE

 

To redirect one page to another page:

Redirect /old-index.html http://www.mynewwebsite.com/foldername/new-index.html

REDIRECT TO ANOTHER WEBSITE

 

To redirect an entire website from one url to another:

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

REDIRECT INDEX TO SUBDIRECTORY

 

To redirect a page to a subdirectory:

Redirect /index.html http://www.mynewwebsite.com/foldername

REDIRECT FILEPATHS

 

To redirect a filepath to another filepath:

Redirect /foldername/filename.html http://www.mynewwebsite.com/foldername2/filename.html

TO CHANGE FILE EXTENTION

 

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

RedirectMatch 301 (.*).html$ http://www.example.com$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 http://www.mysite.com to http://mysite.com:

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

REDIRECT NON-WWW TO WWW

 

To redirect http://mysite.com to http://www.mysite.com:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.mynewwebsite.com$ [NC]
RewriteRule ^(.*)$ http://www.mynewwebsite.com/$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: http://whatismyip.com

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 ^(.*)$ http://www.yoursite.com/307.php [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=http://www.new-website.com" />

To redirect after 5 seconds:

<meta http-equiv="refresh" content="5; url=http://www.new-website.com" />

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).

<?php header ('HTTP/1.1 301 Moved Permanently'); header( "http://www.new-website.com" ); ?> 

See: http://www.htaccessredirect.co.uk/ 

 

Top