Redirect a Web Page
1. 301, "Moved Permanently"
2. 302, "Found" or "Moved Temporarily"
3. HTML Redirect
301, "Moved Permanently"
The HTTP response status code 301 Moved Permanently is used for permanent URL redirection, meaning current links or records using the URL that the response is received for should be updated. The 301 response from the Web server should always include an alternative URL to which redirection should occur. If it does, a Web browser will immediately retry the alternative URL. This is the best way to ensure that users and search engines are directed to the correct page. The 301 status code means that a page has permanently moved to a new location.
302, "Found" or "Moved Temporarily"
The 302 status code indicates that the resource you are requesting has redirected to another resource. With a 302 "Found" or "Moved Temporarily" redirect, the server redirects the user to the new destination despite the original location still being used for requests.
Meta Refresh redirect is a client side redirect and is not 301 permanent redirect. This is the simplest way to redirect to another URL is with the HTML Meta Refresh tag. We can place this meta tag inside the < head > at the top of any HTML page like this:
The content attribute is the delay before the browser redirects to the new page, so here it set to 0 seconds. It is important that the time-out is set to zero, to avoid that content is displayed before the new page is loaded. The page containing the redirect code should only contain information related to the redirect.
Redirecting Pages in HTML 5
The above code will work in the HTML 5 also.
Redirect to current URL with URL parameters
Difference between window.location and location.href
Both are different, window.location is an object containing the property while location.href which is a string. The window.location is an object that holds all information about current document location (host, href, port, protocol etc.). The location.href is shorthand for window.location.href, and this is only string with full url to current website. The location object's toString() value is the same as the href property, so they are identical if used as strings. Setting window.location is the same as setting window.location.href.
URL Redirection on page load
If you want to redirect your web page to other webpage while on page load, you can use the following code. The following program shows a message for 3 second and redirected to new page location.
Please wait....this page will redirect to new page....
Back to Home Page
This is the same as clicking the "Back button" in your browser, or history.go(-1).
How to redirect your website to its mobile version
Note: Normally mobile phones typically have a small screen width, so you should redirect visitors to your mobile site if they have a screen width of less than or equal to 699 pixels.
For iPhones and iPods
Search Engine Optimization and URL redirection
What is the difference between canonical and 301 redirects ?
When you use 301 redirect, you show to Search Engine that current page is permanently moved to another location.
Cannonical is used to prevent penalties by Search Engine for duplicate content. When you use it, the current page exists but shows to Search Engine that is "copy" of another "master page" . From the following example you will get a clear idea about this.
The above 3 url are same and exact content. Normaly Search Engines doesn't like the duplicate content and give penalties too. So in this case you should put in every single page a rel canonical tag to the "master" page. For example we will chose "http://example.com/category-1/subcategory-1/demo-1.html".