/**
*
* DO_REDIRECT
*
* This function will redirect the user if it can resolve that this url request has a redirect.
*
* @author epstudios
*
*/
public function do_redirect()
{
if (is_admin()) return false;
$redirects = self::get_redirects(true); // True for only active redirects.
if (empty($redirects)) return false; // No redirects.
// Get current url
$url_request = self::get_url();
$url_request_path = strtok($url_request, '?');
$query_string = explode('?', $url_request);
$query_string = (isset($query_string[1])) ? $query_string[1] : false;
foreach ($redirects as $redirect) {
$from = urldecode($redirect->url_from);
if ($redirect->status != 'inactive' && rtrim(trim($url_request_path), '/') === self::format_from_url(trim($from))) {
// Match, this needs to be redirected
// increment this hit counter.
self::increment_field($redirect->id, 'count');
if ($redirect->status == '301') {
header('HTTP/1.1 301 Moved Permanently');
} elseif ($redirect->status == '302') {
header('HTTP/1.1 302 Moved Temporarily');
}
$to = ($redirect->type == "url" && !is_numeric($redirect->url_to)) ? urldecode($redirect->url_to) : get_permalink($redirect->url_to);
$to = ($query_string) ? $to . "?" . $query_string : $to;
header('Location: ' . $to, true, (int)$redirect->status);
exit();
}
}
}