2012-11-18 121 views
0

我已經使用Apache mod_rewrite的模塊,我從一個GET請求檢索三個可變的問題,mod_rewrite的條件重定向

$country 
$state 
$location 

我成功的在本地 改寫網址,例如網址

localhost/directory/country/state/location /*is redirected to*/ 
localhost/directory/result.php?c=country&s=state&l=location 

我想要做的是,我要重定向

localhost/directory/country to localhost/directory/country.php?c=country 

並在

localhost/directory/country/state to localhost/directory/state.php?c=country&s=state 

情況下,我使用以下重寫規則

RewriteEngine On 
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ result.php?s=$1&d=$2&l=$3 [L] 

我怎麼可以重寫櫃面國家和國家,並在情況下,如果我想只顯示國家頁..

非常感謝! 請幫我,通過向在線教程和其他參考文獻..

我將不勝感激你爲同一.. :)

+1

複製與較少的佔位符,並與其它/縮短更換的URL規則/ PARAMS。 – mario

回答

1

可以以識別使用向下流動如果URL是一個國家,州,或位置 的東西,如:

<IfModule mod_rewrite.c> 
    Rewrite Engine On 
    RewriteRule ^localhost/directory/([^/\.]+)/([^/\.]+)/([^/\.]+)/ localhost/directory/result.php?c=$1&s=$2&l=$3 [L] 
    RewriteRule ^localhost/directory/([^/\.]+)/([^/\.]+)/ localhost/directory/state.php?c=$1&s=$2 [L] 
    RewriteRule ^localhost/directory/([^/\.]+)/ localhost/directory/country.php?c=$1 [L] 
</IfModule> 

注意如何我已經開始用的時間最長,最有活力的網址,請先。如果你從最短的第一個開始,在你的案例country中,那麼URL_Rewrite將首先接受answer,並且你不會再擊中其他兩個重寫。

儘管我發現在PHP解析端更容易處理一個php頁面上的所有動態URL流量,您的情況就像result.php那樣,您可以確定輸出,而且您不必擔心跳轉在文件上。

.htaccess

<IfModule mod_rewrite.c> 
    Rewrite Engine On 
    RewriteRule ^localhost/directory/([^/\.]+)/([^/\.]+)/([^/\.]+)/ localhost/directory/result.php?c=$1&s=$2&l=$3 [L] 
    RewriteRule ^localhost/directory/([^/\.]+)/([^/\.]+)/ localhost/directory/result.php?c=$1&s=$2 [L] 
    RewriteRule ^localhost/directory/([^/\.]+)/ localhost/directory/result.php?c=$1 [L] 
</IfModule> 

result.php

<?php 
$location = isset($_GET['l']) ? $_GET['l'] : false; 
$state = isset($_GET['s']) ? $_GET['s'] : false; 
$country = isset($_GET['c']) ? $_GET['c'] : false; 

// ...PARSE THE REST OF PHP based off of these variables 
?> 
+0

非常感謝@samuel。我一定會利用你建議的on file變量解析概念......再次感謝 – shashwat

0

我認爲,你可以使用兩個規則:

# localhost/directory/country 
RewriteRule ^[^/]+/([^/]+)$ localhost/directory/country.php?c=$1 [L] 

# localhost/directory/country/state 
RewriteRule ^[^/]+/([^/]+)/([^/]+)$ localhost/directory/state.php?c=$1&s=$2 [L]