0

I am trying to make my own redirect page for external link.

My target

My target is that, when a user open the link www.mysite.com/wp/GoTo/?URL=www.google.com, the browser open the page www.google.com

What I have done

I have saved the following GoTo.php file inside the folder wp-content/themes/theme-name/:

<?php


/*
Template Name: GoTo
*/

/**
 * The template for displaying all pages.
 *
 * This is the template that displays all pages by default.
 * Please note that this is the WordPress construct of pages
 * and that other 'pages' on your WordPress site will use a
 * different template.
 *
 * @package theme-name
 */

$url = $_GET["URL"];

// Remove all illegal characters from a url
$url = filter_var($url, FILTER_SANITIZE_URL);

if (filter_var($url, FILTER_VALIDATE_URL) !== true) {
    header("location: " . $url);
    exit;
} else {
echo("$url is not a valid URL");
}
?>

then I have created a page with the URL www.mysite.com/wp/GoTo/ with template GoTo.

At the moment:

  • if I open www.mysite.com/wp/GoTo/?URL=xyz I get a 404 error (instead of the message "URL is not valid";
  • if I open www.mysite.com/wp/GoTo/?URL=www.google.com I get a 404 error (instead of open google.com).

How can I fix this? Unfortunately I can't refer directly to the php page. In other words I can't write in the browser www.mysite.com/wp/wp-content/themes/theme-name/Z_GoTo.php?URL=www.google.com, as the mod rewrite contains rules against it.

Disclamer

I know that there are several questions about redirect in WP, but none of them is about a situation like mine.

2

The problem is with this expression:

filter_var($url, FILTER_VALIDATE_URL) !== true

which should or could be written as just:

filter_var($url, FILTER_VALIDATE_URL)

Because the PHP manual for filter_var() says:

Returns the filtered data, or FALSE if the filter fails.

which means, with a valid URL, filter_var() does not return a boolean; hence filter_var($url, FILTER_VALIDATE_URL) !== true never evaluates to false. I.e. all URLs would be seen as valid.

So the full code, without the comments:

$url = $_GET["URL"];

// Remove all illegal characters from a url
$url = filter_var($url, FILTER_SANITIZE_URL);

if (filter_var($url, FILTER_VALIDATE_URL)) {
    header("location: " . $url);
    exit;
} else {
    echo("$url is not a valid URL");
}

Additional Note

To redirect to an external URL/domain, you need to specify the protocol — e.g. http://. Try below header() code and you'd understand it:

Code #1:

// Has the protocol; this would always redirect to http://www.google.com
header( 'Location: http://www.google.com' );
exit;

Code #2:

// Protocol not specified; redirects to a URL on your site. For example,
// if you're on http://your-site.com/path, then you'd be redirected to
// http://your-site.com/path/www.google.com
header( 'Location: www.google.com' );
exit;
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.