How to Redirect using PHP, JAVASCRIPT, HTML, .HTACCESS

How to Redirect using Javascript, php, html, htaccess and wordpress plugins

Easy Redirecting Tutorial with the use of PHP, Javascript, HTML and .htaccess

Webmasters are often faced with a situation where one or more files have names that are misfits for the very purpose of the file. You may also feel like there is a need to rename the entire file tree all at once. If that is the case, you will be pleased to know that it is easy to change links in your own site. However, it is a much more tedious process if you want to change the external links to some other site.

Why redirecting is preferable over changing locations?

When the locations of pages are changed, search engines could end up sending users to a broken URL. In more bitter cases, the entire SEO of the page might be reset entirely. The good thing is that there is a rather simple method that makes use of redirects using PHP, JAVASCRIPT, HTML and .htaccess. These allow you to keep the navigational consistency intact and preserve the SEO of the site at the same time, even after you move a few files here and there. In this article, we will show you simple redirect methods using PHP, JAVASCRIPT, HTML, and .htaccess.


Redirect using PHP

If it is a conditional loop, you can redirect your visitor to another page by simply using the following code:

<?php    
header('Location: mypage.php');    
?>

Note that in order for a php to work your page format should be in .php

Please note that in this method, mypage.php is the page address to which you would like to send your visitors. The address could be absolute and may include parameters in the format:

mypage.php?param1=val1¶m2=val2)

For absolute/relative paths

Related  Tutorial : How to Root Android Phone without PC

When toying with absolute or relative paths, it is highly recommended to choose an absolute path straight from the server root (DOCUMENT_ROOT). You will simply need to follow this format:

<?php    
header('Location: /directory/mypage.php');    
?>

In case the target page is hosted on another server you will need to include the complete URL:

<?php    
header('Location: http://www.ccm.net/forum/');    
?>   

Redirect using JavaScript

Dong a page redirect using JavaScript is another very simple way to redirect to another page without changing structure and SEO. If you want to redirect your visitors to a new page, all you need to do us add a line to the head of the code.

<html>   
<head>            
<script type="text/javascript">         
function Redirect() 
{window.location="http://www.tutorialspoint.com";}     
</script>         
</head>      
<body>      
<p>Click the following button, you will be redirected to home page.</p>            
<form>         
<input type="button" value="Redirect Me" onclick="Redirect();" />     
</form>         
</body>
</html>

Using JAVASCRIPT, you can also display a right message to your clients before redirecting them to another page. It generally takes a little time to load a new page. 

Redirecting with HTML

Redirect with HTML is more of a meta redirection code. It is more of a redirect from the client’s end. Nevertheless, it is never a permanent 301 redirect for a page rank transfer in any case. The meta refresh on HTML has a transfer time of flat 0 seconds. It is automatically considered by Google as an indication for a 301-page rank transfer or redirects.

In case, you are looking to solve the permanent 301 redirect, it can be done with the PHP redirect method mentioned above.

Like PHP, the redirection is conducted using by varying the meta refresh in the head section of the page. There is a second insertion of the link in the body section for revisiting purpose. You can use this code:

<!DOCTYPE html>
<html>
<head>
   <!-- HTML meta refresh URL redirection -->
   <meta http-equiv="refresh" content="0; url=http://www.mydomain.com/new-page.html">
</head>
<body>
   <p>The page has moved to:
   <a href="http://www.mydomain.com/new-page.html">this page</a></p>
</body>
</html>

HTML Redirect using Timer

You can also redirect with a timer or redirect by using a specific time you want. Example: You want to redirect or refresh the page on a specific amount of seconds.

Related  How to Watch/Download Movies on Android Phone

This example will redirect page to new-page.html after 5 seconds.

<!DOCTYPE html>
<html>
<head>
   <!-- HTML meta refresh URL redirection -->
   <meta http-equiv="refresh" content="5; url=http://www.mydomain.com/new-page.html">
</head>
<body>
   <p>The page has moved to:
   <a href="http://www.mydomain.com/new-page.html">this page</a></p>
</body>
</html>

Redirect using .htaccess

It is fairly simple to redirect to a page by using .htaccess. All you need to do is:

  1. Make an empty file using one of the text editors and name the file as htaccess.txt.
  2. Edit the text in the file.

(Permanent) 301 Redirect: In this way, you can point the whole site to another address permanently. This is a common redirect method that can be used in most of the situations. Here’s the code:

Redirect 301 /http://example.com/

(Temporary) 302 redirect: The entire site gets redirected to a temporary new location. This is great if you have SEO on the mind and are going to send the page to a new landing page. In this manner, you can switch back to the original page anytime you want.


Redirect using a WordPress plugins

This is by far the simplest way to manage a redirect if you are running a WordPress site. Here are two simple plugins that can do the job for you.

  1. Redirection

This is the simplest way to introduce and manage your redirects on a WordPress website. All you need to do is install the plugin and activate it. Once the plugin is activated, go to the Tools and then Redirection so that your redirects are set up.

  1. Page Links To

This is another extremely simple plugin that allows you to retain a current page and then link it to another page or post. All you need to do is install and activate the plugin, roll down to the bottom of a page and check the “Page Links To” box. Enter the landing page of your choice.

Leave a reply:

Your email address will not be published.

Site Footer

Sliding Sidebar