PHP重定向与参数转发

发布于 2025-01-19 21:27:47 字数 1276 浏览 2 评论 0原文

我正在使用以下PHP脚本来创建重定向:

<?php
 
$id     = isset( $_GET['id'] ) ? rtrim( trim( $_GET['id'] ), '/' ) : 'default';
$f  = fopen( 'redirects.txt', 'r' );
$urls   = array();
 
// The file didn't open correctly.
if ( !$f ) {
    echo 'Make sure you create your redirects.txt file and that it\'s readable by the redirect script.';
    die;
}
 
// Read the input file and parse it into an array
while( $data = fgetcsv( $f ) ) {
    if ( !isset( $data[0] ) || !isset( $data[1] ) )
        continue;
    
    $key = trim( $data[0] );
    $val = trim( $data[1] );
    $urls[ $key ] = $val;
}
 
// Check if the given ID is set, if it is, set the URL to that, if not, default
$url = ( isset( $urls[ $id ] ) ) ? $urls[ $id ] : ( isset( $urls[ 'default' ] ) ? $urls[ 'default' ] : false );

if ( $url ) {
    header( "X-Robots-Tag: noindex, nofollow", true );
    header( "Location: " .  $url, 302 );
    die;    
} else {
    echo '<p>Make sure yor redirects.txt file contains a default value, syntax:</p>
    <pre>default,http://example.com</pre>
    <p>Where you should replace example.com with your domain.</p>';
}

它运行良好,除了URL参数(示例:domaine.com/redirect/target-url?param=ABC均未转发...和我希望它们是伪造的

I am using the following PHP script in order to create redirects :

<?php
 
$id     = isset( $_GET['id'] ) ? rtrim( trim( $_GET['id'] ), '/' ) : 'default';
$f  = fopen( 'redirects.txt', 'r' );
$urls   = array();
 
// The file didn't open correctly.
if ( !$f ) {
    echo 'Make sure you create your redirects.txt file and that it\'s readable by the redirect script.';
    die;
}
 
// Read the input file and parse it into an array
while( $data = fgetcsv( $f ) ) {
    if ( !isset( $data[0] ) || !isset( $data[1] ) )
        continue;
    
    $key = trim( $data[0] );
    $val = trim( $data[1] );
    $urls[ $key ] = $val;
}
 
// Check if the given ID is set, if it is, set the URL to that, if not, default
$url = ( isset( $urls[ $id ] ) ) ? $urls[ $id ] : ( isset( $urls[ 'default' ] ) ? $urls[ 'default' ] : false );

if ( $url ) {
    header( "X-Robots-Tag: noindex, nofollow", true );
    header( "Location: " .  $url, 302 );
    die;    
} else {
    echo '<p>Make sure yor redirects.txt file contains a default value, syntax:</p>
    <pre>default,http://example.com</pre>
    <p>Where you should replace example.com with your domain.</p>';
}

It works well except that URL parameters (example : domaine.com/redirect/target-url?param=abc are not forwarded... And I'd like them to be forwared.

How can I amend this code so URL parameters are forwarded ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小嗲 2025-01-26 21:27:47

您可以使用http_build_query()创建一个查询字符串,并为其馈送一个键=&gt的数组;值。使用$ _ get将转发所有参数:

$params = http_build_query($_GET);
$url = $url.'?'.$params;

如果您需要删除某些参数(即ID),则可以创建$ _ GET的副本然后揭开您不想转发的项目:

$params = $_GET;
unset($params['id']);
$url = $url.'?'.http_build_query($params);

You can create a query string using http_build_query() and feed it an array of key => values. Using $_GET would forward all the parameters:

$params = http_build_query($_GET);
$url = $url.'?'.$params;

If you need to remove some params (ie id), you can create a copy of $_GET and then unset the items you don't want forwarded:

$params = $_GET;
unset($params['id']);
$url = $url.'?'.http_build_query($params);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文