Monday, 4 May 2015

How can I make a redirect page using javascript jQuery PHP and htaccess

How can I make a redirect page using javascript jQuery PHP and htaccess

Redirection with JavaScript OR jQuery.

For redirect a page to another page in client side you can use window.location. For this you need not to include the jQuery file.

Question: What is window.location?
The window.location is a object which have all the values like host, href, port and username etc of current location. It is used to get the current page address (URL) and to redirect to a new page.

When I do console.log(JSON.stringify(window.location)), It print following data.
 
{"href":"http://www.web-technology-experts-notes.in/2014/10/jquery-technology.html","origin":"http://www.web-technology-experts-notes.in","protocol":"http:","username":"","password":"","host":"www.web-technology-experts-notes.in","hostname":"www.web-technology-experts-notes.in","port":"","pathname":"/2014/10/jquery-technology.html","search":"","hash":""}

To use the window.location, Its not necessary to include the jQuery.



Question: What is difference between window.location and window.location.href?
window.location is a object which have all the values of current location. I can be used to get the current url and redirect to another website OR page.

window.location.href is one of the property of window.location which is used to get the current url and redirect to another website OR page.


Question: How to redirect a page.
window.location="http://web-technology-experts-notes.in";



Question: How to redirect a page using javascript on onclick event.
 <a href="javascript:void(0);" onclick="redirectPage()">Click to Redirect</a>
 
function redirectPage(){
    window.location="http://web-technology-experts-notes.in";
}


Redirection with PHP

Question: How to redirect to another page.
header("Location: /another-page.php");


Question: How to redirect to another website.
header("Location: http://web-technology-experts-notes.in");


Question: How to redirect to another website temporary.
header("Location: http://web-technology-experts-notes.in", true, 302);


Question: How to redirect to another website permanent.
header("Location: http://web-technology-experts-notes.in", true, 301);


Following are the header codes and their values.
301 - Permanent movement
302 - Temporary movement
400 - Bad request
401 - Authorization Required
403 - Forbidden
404 - Page Not Found
500 - Internal Server Error



Redirection with htaccess

Question: How to redirect to another page directory.
Redirect / /newdir

Question: How to redirect to another page.
Redirect /about.html /pages/about