2013-07-22 43 views
0

我有了改變位置的位置菜單,好事是在每個城市每個URL存在,,並且每個城市都是一個子域刪除子域名,URL飼養的其餘部分(更換子域)

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

我試着這樣做。我試着去打破網址刪除子域名,這樣我就可以在菜單取代它的每個項目

我想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 
+0

不要那樣做。使用http://php.net/parse_url –

+0

我正在使用解析,我只能刪除http:// www。 (基於此http://stackoverflow.com/questions/6667086/remove-protocl-and-subdomain-from-url)(¿我怎樣才能更改www。在任何子域的代碼不只是www。? –

回答

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; 

乾杯!

+0

明白了!我只是從您的代碼中刪除「/」 –

+0

請參閱編輯的答案希望可以幫助:) – Konsole

+0

哦,很好的想法..謝謝 –

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