Close

Not a member yet? Register now and get started.

lock and key

Sign in to your account.

Account Login

Forgot your password?

301 & 302 redirects: using php or htaccess

What is the difference between 301 and 302 redirect codes?

 

Code 301 is used for permanent redirects, meaning that page, directory or the whole site have moved permanently, for ever – and both users and search engines have to use new link address from now on. This can apply to the situation when you have changed your site domain or changed your site structure and as a result old content is accessible now by new link address.

 

Code 302 is used for temporarily redirects, meaning that in the future old url could be in use again. This can apply to the situation when you are doing some kind of promotion on your site or doing some changes that are not final.

 

Redirects are important because they help you to avoid 404 (not found) errors on your site, helping your visitors find the content they were looking for. That also helps your site to preserve search engines (google and others) rating and thus save your site popularity.

 

So how do you do the redirects? There are two ways to do it if your sites are running apache+php – via htaccess file or in the php script itself.

 

1) It will work faster if you decide to do the redirect via the htaccess file and it is quite easy to do. Here is the code:

redirect 301 /old_folder_pass/old_file.html http://www.newdomain/new_folder_pass/new_file.html

There is no need to specify your domain name in the old link, however the OLD domain should be pointing to the directory with this htaccess file. The new domain should be specified unless it is the same as the old one (if you just changed the site link structure, but did not change the domain name).

In case the redirect should be temporal, you can just substitute 301 with 302:

redirect 302 /old_folder_pass/old_file.html /temp_folder_pass/temp_file.html

To see more examples and techniques on using htaccess redirects, please refer to this site: http://www.isitebuild.com/301-redirect.htm

redirect request

 

2) The second way is to do a redirect using a script language, such as PHP. It will work a little bit slower (since a PHP script should be executed before user will be redirected to a new link), but is more flexible.

 

It is recommended by search engines that in case you move you site to a new domain or change links structure, ALL links that existed on your old site should point to the links on the new site. And that users should not just be redirected to the home page, but to the page with corresponding content. It can be difficult to achieve using htaccess redirect, if you have a dynamic website, link structure changed a lot and you have to refer to the database to understand the mapping between the old and new links. In this case one can use PHP for handling redirects. You can use this samle of the code to make redirects via PHP:

//some code that does mapping of old URL to the new one, possible calls to the database
header(“HTTP/1.1 301 Moved Permanently”); //use this for 301 redirects only, in case you want to do temporary redirect, nothing is needed
header( ‘Location: http://newdomain.com/newurl’ );
exit();

Please make sure that no HTML is returned by the script before the header(); function is called, otherwise the redirect won’t work.

 

There is a nice website that allow you to check if your redirects are working correctly: http://www.internetofficer.com/seo-tool/redirect-check/ . Of course, you can check if redirects are working just by typing your old address in the browser, but this tool will also let you know what type of redirect (301 vs 302) is used in your particular case.

  1. website seo08-24-13

    Options +FollowSymLinks
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^exemple.com [NC]
    RewriteRule ^(.*)$ http://www.exemple.com/$1 [L,R=301]

  1. cloudinservice.com RSS Feed10-02-14