0

我有一个必须更改位置的位置菜单,好在每个城市都存在每个 url,每个城市都是一个子域

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

此外,我还没有考虑解决我将面临的问题..

如果我在看一个产品,那么 url 就会变成类似的东西

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

但是,这些产品并不是每个城市都有,所以我的用户会得到一个 404。

=(

我如何在此过程中设置条件,所以如果page=item/preview/25 我将其替换为

page=index/index
4

2 回答 2

1

You can split the domain as:

$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;

Cheers!

于 2013-07-22T15:55:45.053 回答
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;
?>
于 2013-07-22T16:30:11.937 回答