00:00:00
02 May

PHP: Redirect To Url – Delayed, With Or Without “Header”, After Form Submission, Cloaked Affiliate Redirects

Oliver Krautscheid Nov 26, 2014
0 1709

PHP makes redirecting users very easy. There are countless ways how you can redirect visitors to other pages. Let’s take a look at a few examples

Php Redirects Explained

Redirect to new website using header

The easiest method to redirect visitors is by using “header” wrapped in PHP tags right before the DOCTYPE tag.

header("url=http://domain.com/");  

Redirect after X seconds

If you want to redirect visitors after reading a redirect message or similar, you can simply append refresh:X where X is the time in seconds.

header("refresh:15;url=http://domain.com/");  

This will delay the redirect for 15 seconds. 3-5 seconds is what I would recommend if you only want to display a quick message. That would be sufficient for your visitors to scan the message and move on.

Redirect passing URL as session variable

One good way to redirect is to use a bridge PHP file called out.php or 1st.php

Insert into your bridge file e.g. out.php:

$url= $_GET['url'];  
session_start();
$_SESSION['firstURL'] = $url;
header("refresh:1;url=http://domain.com/frame.php" );  

Then in your frame.php or 2nd.php file insert:

session_start();
$posturl = $_SESSION['firstURL'];  
<iframe height="1024" id="preview-frame" src="<?php echo $posturl;?>" name="preview-frame" frameborder="0" noresize="noresize"></iframe>

Redirect To URL Without Using Header

You can use a meta refresh

<META http-equiv="refresh" content="0;URL=http://webmaster.net">

Please note that HTML meta refreshes are not very user-friendly. Users can disable them in their browser. Actual PHP redirects cannot be disabled and will be enforced.

Redirect To URL Using HTACCESS Mod_Rewrite

The most trivial way to redirect any given URL is to simply use Apache’s Mod_Rewrite. You should already have a htaccess file.

Simply insert this at the top of the file:

Redirect /post-article.html https://www.webmaster.net/new-article/

Here’s another example redirecting a page to a different domain:

Redirect /post-article/ http://www.domain.com/new-article.html

And another for a file called creative.php

Redirect /creative.php http://www.domain.com/offer/store

Cloaking Affiliate Links

Affiliate links are never pretty and should be cloaked. This is perfectly user and SEO-friendly and may even cause less damage than putting real affiliate links on your website. Google will still detect the redirect, but you are only indirectly linking to a different domain and can disallow the bridge file in your robots.txt

Example:

webpage.html

Redirect to /creatives/bridge.php => Redirect to actual offer

  if (isset($_COOKIE['tracking202outbound'])) {
	$tracking202outbound = $_COOKIE['tracking202outbound'];     
  } else {
	$tracking202outbound = 'http://dn.com/tracking202/redirect/lp.php?lpip=753&pci='.$_COOKIE['tracking202pci'];
  }
header('location: '.$tracking202outbound);

This is an example straight from Prosper202, which is one of the leading affiliate tracking systems on the market.

Robots.txt

Disallow: /creatives/* 

Redirect After Form Submission

1. Create your form

2. At the bottom of your form directly below the input type=”submit” insert this:

 
if(isset($_POST['submit'])){
 if (!empty($_POST['value'])){
 header("Location:http://www.webmaster.net/success/");
 }
}

This will only redirect after a user has hit the button submit AND only if the variable “value” is not empty.

You may have to insert a few more checks to make sure all of your fields are entered properly and you may want to insert an else statement as well to display an error message. That is up to you.

Conclusion

There are many ways to redirect visitors and many reasons why you should know everything there is about redirects.

Please do not try to overdo it, Google is very picky and does not endorse Sneaky redirects, Gateway redirects, multiple requests on top of each other and other forms of confusing the visitor. Google is capable of detecting all malicious redirects. You can use bridge files to redirect visitors to affiliate offers without a problem, just don’t try to trick your users into something they don’t want to click on.

The most common purpose of bridge files is for external links and download pages.

Download pages often use “header” to initiate file downloads using MIME-TYPES. Without a bridge file this is not possible.

It is also a good idea to use a bridge file for offers or links you do not want to link to directly.

About Us

Webmaster .Net is your all-in-one resource for blogging tips, system administration guides, industry news and a growing community of webmasters.

Can't find something or want to tip us?Contact@webmaster.net

Bulls and Bears
Webmaster Tutorials, Wordpress
0 shares663 views

WordPress SQL Query: Select And Delete Posts Between Two Dates

Oliver Krautscheid - Apr 08, 2017

If you are wondering how to delete posts for a certain date, you will need to know how Wordpress stores…

Advertising and Ad Networks, Copywriting
0 shares806 views

15 Tips For Increasing Facebook Ads CTR – Full Guide [2017 Update]

Cam Connor - Mar 30, 2017

In this article, we'll talk a lot about how you can write quality text ads on Facebook Ads to get…

Webmaster Tutorials
0 shares1484 views

10 Steps To Configure Nginx For WordPress, Drop Varnish And Cut Your CPU Load By 50%

Oliver Krautscheid - Oct 10, 2016

Are you using Varnish on your Wordpress blog? Possibly the Varnish script by Unixy? Great! Get rid off it. Here's…

Follow Us And get latest news

© 2016 Webmaster.Net - Property of Peakstone Media Ltd. - All rights reserved. | Privacy Policy | DMCA | Contact Us | Advertise