开发者

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 (i.e., ^/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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜