2017-07-03 89 views
0

我正在尋找,但我無法做到這一點。.htaccess - 刪除index.php,使用多個參數重定向

我有以下.htacess

RewriteEngine on 

# Rewrite request URL 
# Input: index/VIDEO/ 
# Output: index.php?id=VIDEO 
RewriteRule ^(\w+)/?$ index.php?id=$1 

它做工精細改變以下網址:

https://subdomain.domain.com/path/to/index.php?id=234556 

https://subdomain.domain.com/path/to/234556 

但我已經添加了第二個參數(許可證),所以我需要重寫以下URL:

https://subdomain.domain.com/path/to/index.php?id=234556&license=23432532 

https://subdomain.domain.com/path/to/234556/23432532 

或者

https://subdomain.domain.com/path/to/234556&license=23432532 

我一直在努力通過多種方式在這裏尋找,但我不能做這項工作。

回答

1

您可以使用:

RewriteRule ^([^/.]+)/(.*?)$ /index.php?id=$1&license=$2 [L,QSA] 

其中,第一個參數包含所有的值,直到它找到/。下一個參數保存全部。您也可以多次重複第一個表達式。


替代方法:

但是,當你的應用需要,需要進行動態處理一些更多的搜索參數,你可以利用REQUEST_URI在PHP。

在這種情況下,你的RewriteRule可以像:

RewriteRule^index.php [L] 

而在PHP中,你可以檢索使用$_SERVER['REQUEST_URI']所有值:

if (isset($_SERVER['REQUEST_URI'])) 
{ 
    $params = explode("/", ltrim($_SERVER['REQUEST_URI'], "/")); 
    print_r($params); 
} 

見另一SO Answer。這樣,其他頁面可以爲不同目的使用相同的重寫規則。

+0

感謝您的回答。我得到「對象在Xampp沒有位置」。我使用RewriteRule ^([^ /。] +)/(。*?)$ /index.php?id=$1&license=$2 [L,QSA] Url to test: http:// localhost/api/source/videos/28652187/23432324 – Kokox

+1

在'index.php'之前刪除'/'。試試這樣:''RewriteRule ^([^ /。] +)/(。*?)$ index.php?id = $ 1&license = $ 2 [L,QSA]' – Thamilan

+0

順便說一句,我無法測試你的'localhost' URL :) – Thamilan