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.