How to Redirect a Web Page (HTML, Apache and PHP redirects)

Redirecting is the process to take the visitors to a new page when they visit a certain expired or non-existing webpage. Redirecting changes the old URL to a new URL.

For example: I had redirected this website from “http://tech.xnepali.com” to “http:computernewsreport.com“. That was domain-level redirect.

In this website, if you click this link “computernewsreport.com/how” it automatically changes to “computernewsreport.com/how-to-get-more-followers-in-twitter”. That is, site-level redirect.

I will discuss the following different methods for redirecting (you can choose only one). If you are unsure of which one to choose, just go with HTML Redirect because, it is the easiest and has less impact in the site configurations:

  • HTML Redirect
  • Apache Redirects
  • PHP Redirects

HTML Redirects

Meta Refresh tag can be used to redirect from an existing page to a new location. In the HTML file of the old page, locate the <head> section at the top and insert the following redirecting code:

<meta http-equiv="refresh" content="0; URL='http://new-domain.com/sub-page/'" />

If you are redirecting domain root (main page) to another domain root, past the above code in the index.html or index.htm file of the domain folder replace the “new-domain.com/sub-page/” by your new domain name.

Apache Redirects

As most of the websites are hosted on Apache servers, Apache redirects is a very common method of redirecting a web page. For this a file “.htaccess” found on the Apache web server should be edited and added a code. The file “.htaccess” is where the commands to be given to the server are saved.

To redirect the website or sub-folders, new .htaccess file can be created. If there is already a “.htaccess” file located in the root directory or sub-folder, it should be opened and the following code should be added:

Redirect 301 / http://www.new-website.com

Redirecting a sub-folder (blog or website) to a new domain:
RedirectMatch 301 /sub_folder(.*) http://www.new-website.com$1

Redirecting individual pages can be done by

Redirect 301 /things.htm http://www.new-website/thigies.htm

PHP Redirects

A “header’ function can be used in PHP to redirect websites. The following code should be used in the top of the PHP file like “index.php” or something like to redirect to the new website.

<?php
header('Location: http://new-website.com', true, 301);
exit;
?>

Advertisement

Leave a Reply

Your email address will not be published. Required fields are marked *