Top 5 Tricks to Perform Redirection with PHP ~ Coding Savvy
Coding Savvy FB Twitter Google
» »

Top 5 Tricks to Perform Redirection with PHP

A server side redirect is a method of URL redirection using an HTTP status code (e.g., 301 Moved Permanently, 303 See Other and 307 Temporary Redirect) issued by a web server in response to a request for a particular URL. The result is to redirect user's web browser to another web page with a different URL. Redirection in PHP has lots of approaches, to be more specific about it, everything boils down to what you want. We are going to discuss How to redirect visitors using PHP programming language. This are the 5 most common ways to perform redirection on your website with PHP. We are going to see how we can use JavaScript also .htaccess file for redirection.
Top 5 Most Secured Ways Perform Redirection with PHP

1. Using PHP location header

This is the fastest way of redirecting users with PHP. It is as simple as this:
The code above will redirect users to thankyou.php file in the root domain. but this will not work if an header has already been set, i.e an example as below will not work
echo "hello";
The case above won't work because the page header has already been set in line 1.

2. Location header with Timeout

This difference between the and the top 1 is that, You will set a waiting time for the server before it redirects, see below:
header( "refresh:5;url=thankyou.php" ); 
The code above will redirect users after 5 Seconds.

3. Redirect with JavaScript

The third type of redirection is with the support of JavaScript, This is only recommended in occasions when javascript is enabled by the user.
echo "<script> window.location='thankyou.php'; </script>";
The method above will not work if javascript is disabled so, remember to check if java script is enabled before use.

4. Redirection using the .htaccess file

.htaccess file is an Apache file placed in the root domain, according to wikipedia, A .htaccess (hypertext access) file is a directory-level configuration file supported by several web servers, used for configuration of site-access issues, such as URL redirection, URL shortening, Access-security control (for different webpages and files), and more. by creating an htaccess file (if it doesn't already exist ) you can perform many type of redirection.
  301 (Permanent) Redirect: Point an entire site to a different URL on a permanent basis. This is the most common type of redirect and is useful in most situations. In this example, we are redirecting to the "" domain:

# This allows you to redirect your entire website to any other domain
Redirect 301 /

302 (Temporary) Redirect: Point an entire site to a different temporary URL. This is useful for SEO purposes when you have a temporary landing page and plan to switch back to your main landing page at a later date:
# This allows you to redirect your entire website to any other domain
Redirect 302 /
Redirect index.html to a specific subfolder:
# This allows you to redirect index.html to a specific subfolder
Redirect /index.html
Redirect an old file to a new file path:
# Redirect old file path to new file path
Redirect /olddirectory/oldfile.html
Redirect to a specific index page:
# Provide Specific Index Page (Set the default handler) 
DirectoryIndex index.html
Note that htaccess file must be name .htaccess without extension and uploaded to the root directory.

5 . a tag Manual Redirection

This type of redirection is possible with user triggered action by clicking a button, This is done by using the HTML a tag, This will allow user to click whenever they are ready to be redirected.
Welcome James,
echo "<a  href="home.html" alt="Home" title="Home Page" > Click here</a> to vist the home page.";
This will not redirect until the user clicks the click here button.

Happy Coding (:
Was this article helpful?
Thanks! Your feedback helps us improve tutorials.

You May Also Like...

No comments:

Post a Comment