Friday, 23 February 2018

PHP Redirect, 301 or 302 Redirection With PHP and benefits

301 or 302 Redirection With PHP and benefits

Question: What is Redirection?
On a Web site, redirection is a technique for moving visitors to a different web page OR Different website.


Question: Why we do need redirection?
We do need redirection, because we want to tell the visitor that current page is not appropriate for you or current page have been moved to new location.

Following are different reason, we do Redirection
  1. If user already login, and try to open the login page redirect him to user dashboard.
  2. Trying to access a page which is temporary not available.
  3. If user trying to access un-authorized page, Redirect him to user dashboard.
  4. Tring to access non-exist page, redirect him sitemap page OR 404 page.
  5. Trying to access non-exist website.


Question: What are different type of redirects?
  1. 301 permanent redirects
  2. 302 temporary redirects


Question: What is 301 permanent redirect?
301 code refers to the HTTP status code.
A 301 redirect is a permanent redirect which passes between ranking power(>90%) to the redirected page.


Question: When we use 301 redirects?
  1. When a web page permanently removed, we redirect user to new page.
  2. When we want to transfer the ranking benefits(SEO point of view) to new web page.
  3. Redirect user to new web page and stopped him to access old page(not available page).
  4. If you have updated the contents to new web page, MUST USE 301 Redirects
  5. 301 indicates to both browsers and search engine bots(Google, bing, yahoo) that the page has moved permanently.



Question: What is 302 temporary redirects?
302 refers to the HTTP status code.
A 302 redirect is a temporary redirect. We have to tell user its temporary unavailable and will be available very soon.


Question: When we use 302 redirects?
  1. If user already login, and try to open the login page redirect him to user dashboard.
  2. Trying to access a page which is temporary not available.
  3. If user trying to access un-authorized page, Redirect him to user dashboard.


Question: How to make 301 redirect with php (Permanent)?
header('Location: http://www.example.com/new-web-page.php', true, 301);exit;



Question: How to make 302 redirect with php (Temp)?
header('Location: http://www.example.com/new-web-page.php', true, 302);exit;

OR
header('Location: http://www.example.com/new-web-page.php');exit;

Both are same and works same.


Question: Can we use regular express in Redirection?
Yes, See Example:
if(preg_match("/^\/old\./", $_SERVER['REQUEST_URI'], $m)){
    header('Location: http://www.example.com/new-web-page.php', true, 301);exit;
}
URL start with /old will be redirect to http://www.example.com/new-web-page.php


Question: Give an URL to check 301 redirects?
http://www.aboutcity.net/youtube/videos/watch/wEVPkxFC0NI


Question: Where can I test redirect online? http://www.redirect-checker.org/index.php
(Check Your Redirects and Statuscode 301 vs 302, meta refresh & javascript redirects)


Question: How to make 301 redirect with htaccess?
Add following code in htaccess
Redirect 301 /old/ /new/

Make Sure "RewriteEngine On" and installed "mod_rewrite" on server.


Question: How to make 302 redirect with htaccess?
Add following code in htaccess
Redirect 302 /old/ /new/

Make Sure "RewriteEngine On" and installed "mod_rewrite" on server.

Question: What are different HTTP status code used in web?
301 - Permanent movement(redirection)
302 - Temporary movement(redirection)
400 - Bad request
401 - Authorization Required
For More details http://www.web-technology-experts-notes.in/2014/02/htaccess-code-snippets-example.html