If you are a webmaster, it is likely that there will be one time or another you’ll need to redirect your website, webpage, or a list of webpages to another site, another page or a pattern of page.

There are many ways to redirect, most standard way is to use .htaccess file (which is typically put in the public_html folder). However, .htaccess is a difficult coding language and can be very troublesome if your redirect is a complicated pattern.

Fortunately, we can also use PHP to perform any kind of redirect. PHP is a simple, flexible and very easy to learn. You just need to put the following piece of code at the bottom of the function.php file within your active theme folder, to testout how it works:

 

//Redirect all urls to the home page of example.com, that is http://yourdomain.com/sample-page.html to http://example.com
header(‘Location: http://example.com’);
exit;

//Redirect url of this domain to the corresponding url of example.com, that is http://yourdomain.com/sample-page.html to http://example.com/sample-page.html
//header(‘Location: http://example.com’.$_SERVER[‘REQUEST_URI’]);
exit;