2017-07-03 91 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