简体   繁体   中英

PHP redirect to another website

I have a directory named "goto" and a file inside called index.php. Currently the following is inside the index.php file:

<?php
$url = $_GET['url'];
header("Location: $url");
?>

At the moment to redirect to another URL I have to type this into the address bar:

http://mysite.com/goto/?url=http://google.com

I would appreciate it if you could tell me how I could change that URL so that I could redirect the user to a website by typing this into the address bar:

http://mysite.com/goto/http://google.com

Use mod_rewrite and .htaccess to rewrite http://mysite.com/goto/http://google.com as http://mysite.com/goto/?url=http://google.com

RewriteEngine On
RewriteRule ^goto/(.+)$ /goto/?url=$1 [L]

Depending on your server configuration you may need to include a / in your rewrite path (ie, ^/goto/(.+)$ ).

Unless you want to become a malware hub, I would wholeheartedly recommend you not doing this.

If you wish to allow redirect in such a manner, using http://mysite.com/goto/google and then work out the domain from a whitelist of available, allowed, destinations.

You will need to parse the data which could be a little tricky because you have to differentiate the difference between your URL and the other URL.

My suggestion is to not do so because the second that header is launched you will not see the url and it be better for you to just pass it as a get statement or a post.

EDIT

If you're determined then parse_url() is what you want. :)

@ide's method would work ... but you could also have the PHP script examine $_SERVER['PATH_INFO'] , which is how that part of the URL would get passed to the CGI script.

(although, if there's a question mark in there, you'll also have to either make sure it's URI encoded, or also get the QUERY_STRING; you'll also lose any part after a hash, but you'd have the same problem with your current scheme)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM