301 Redirect

How to 301 Redirect in PHP , HTML and htaccess

The 301 redirection of a page to another one can be done in many ways, this article will mention and explain some of them.

1. htaccess 301 REDIRECT

You can always 301 redirect a page to another page by creating a htaccess file (the file name should be .htaccess) and adding the following code to it:

Redirect 301 / http://www.yoursite.com/page.html

Change / to the path of your page you want to 301 redirect and http://www.yoursite.com/page.html to target page. The code mentioned above will redirect the homepage of your site to another page at the mentioned URL.

This is another example of 301 redirecting

Redirect 301 /myoldpage.html http://www.yoursite.com/mynewpage.html

This code will 301 redirect “myoldpage.html” to the page called “mynewpage.html” in the URL above.

2. PHP Redirect 301

You may also 301 redirect a page by adding the following PHP code to the top of the PHP file:

<?php header("Location: yourpage.php"); ?>

Change yourpage.php to the target page (the new page) , You may also replace it with a URL if you like to redirect it to external page.

Notice: The mentioned code have to be written before the first print command!

3 – HTML Redirect 301

In the same way as PHP redirection you can also add the following HTML code to the page you want to HTML redirect

<meta http-equiv="refresh" content="0; url=http://yoursite.com">

The HTML redirection code is somewhat more complex than the PHP one, In fact it has an option to redirect the page after some time of loading the original page.

Differences Between the 301 Redirection Methods

The main difference between three redirection ways mentioned above is the redirection level. A htaccess 301 redirect will redirect to the new page without even allowing the visitor to visit it, That’s because htaccess files are complied by a mod called mod_rewrite in the Apache web-server, so the server will redirect to the new page without loading the old page.

With PHP and HTML Redirection, the server will only realize that it has to redirect to the new page only when loading the old page!

This article is about: HTML redirect How to HTML Redirect PHP redirect How to PHP redirect htaccess 301 redirect – 301 redirect how to 301 redirect