在PHP中检查url是否包含字符串和查询字符串的最佳方法

在PHP中检查url是否包含字符串和查询字符串的最佳方法,php,query-string,Php,Query String,我试图检测url是否包含字符串或带有querystring的字符串,然后给它一个不同的函数 下面的代码是我能想到的全部,但它不起作用 检查这个的最好方法是什么 url 1 = http://example.com/index.php url 2 = http://example.com/index.php?some-test-page if (stripos($_SERVER['REQUEST_URI'], 'index')){ $xurl="http://example2.com/

我试图检测url是否包含字符串或带有querystring的字符串,然后给它一个不同的函数

下面的代码是我能想到的全部,但它不起作用

检查这个的最好方法是什么

url 1 = http://example.com/index.php

url 2 = http://example.com/index.php?some-test-page

if (stripos($_SERVER['REQUEST_URI'], 'index')){
    $xurl="http://example2.com/photos/";
} else {
    $req_url = $_SERVER['QUERY_STRING'];
    $xurl=('http://example2.com/photos/'   . $req_url . '');
}

您应该检查requesturi中的“?”而不是索引

url 1 = http://example.com/index.php
url 2 = http://example.com/index.php?some-test-page

if (stripos($_SERVER['REQUEST_URI'], '?') === false ){

  $xurl="http://example2.com/photos/";

    }
else{
    $req_url = $_SERVER['QUERY_STRING'];

    $xurl=('http://example2.com/photos/'   . $req_url . '');
    }

如果有查询字符串,它将填充。只要检查一下,并在需要时使用添加回来(这将确保任何逃逸都得到处理)


使用parse_URL将URL拆分为多个部分,然后仔细检查您需要的内容。。。

如果有查询字符串,我想像这样附加它,那么最好对问题进行解释
$xurl = "http://example2.com/photos/";
if (!empty($_GET)) {
    $xurl .= "?" . http_build_query($_GET);
}