从URL字符串中删除子域,保留URL的其余部分(用于替换子域)

时间:2013-07-22 15:42:35

标签: php parsing explode

我有一个位置菜单,必须更改位置,好处是每个城市都存在每个网址,而且每个城市都是子网域

city1.domain.com.uk/index.php?page=category/238/12
city2.domain.com.uk/index.php?page=category/238/12

我试着这个。我试图打破URL删除子域名,所以我可以替换菜单中的每个项目

我想获得index.php?page=category/238/12

<?PHP
   $protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https')=== FALSE ? 'http' : 'https';
$host     = $_SERVER['HTTP_HOST'];
$script   = $_SERVER['SCRIPT_NAME'];
$params   = $_SERVER['QUERY_STRING'];

$url = $protocol . '://' . $host . $script . '?' . $params;


   // break it up using the "." 
$urlb = explode('.',$url);

// get the domain 
$dns = $urlb[count($urlb)-1];

// get the extension 
$ext = $urlb[count($urlb)+0];

//put it back together 
$fullDomain = $dns.'.'.$ext; 

  echo $fullDomain;


   ?>

但是我得到了这个php?page=category/238/12

此外,我还没有想到一个问题的解决方案,我将面临这个问题..

如果我正在查看产品,则网址会更改为

 city2.domain.com.uk/index.php?page=item/preview/25

但是,产品并不存在于每个城市,因此我的用户将获得404。

=(

如何在此过程中设置条件,以便page=item/preview/25我替换

page=index/index

2 个答案:

答案 0 :(得分:1)

您可以将域拆分为:

$url = "city1.domain.com.uk/index.php?page=category/238/12";

 list($subDomain, $params) = explode('?', $url);
 list($domain, $sub) = explode('/', $subDomain);

 $newUrl = $sub . "?" . $params;

 echo $newUrl;

干杯!

答案 1 :(得分:0)

这个怎么样:

 <?php
  $protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https')=== FALSE ? 'http' : 'https';
  $host     = $_SERVER['HTTP_HOST'];
  $script   = $_SERVER['SCRIPT_NAME'];
  $params   = $_SERVER['QUERY_STRING'];
  $url = $protocol . '://' . $host . $script . '?' . $params;
  $url=(parse_url($url));
  $dns = substr($url['host'],stripos($url['host'],'.')+1);
  $fullDomain =$url['scheme']."://".$dns.$url['path']."?".$url['query'].$url['fragment'];
  if (substr($url['query'],stripos($url['query'],'=')+1,stripos($url['query'],'/')-stripos($url['query'],'=')-1)=='item') {
    echo "redirect";
  } else {
    echo "don't redirect";
  }
  echo "<br>".$fullDomain;
?>