.htaccess - 删除index.php,使用多个参数重定向

时间:2017-07-03 04:37:00

标签: php apache .htaccess mod-rewrite parameters

我正在搜索,但我无法工作。

我有以下.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

但是我添加了第二个参数(许可证),所以我需要重写以下网址:

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 个答案:

答案 0 :(得分:1)

您可以使用:

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

其中,第一个参数保存所有值,直到找到/。下一个参数包含所有。您也可以多次重复第一个表达式。

替代方法:

但是当您的应用程序需要更多需要动态处理的搜索参数时,您可以在PHP中使用REQUEST_URI

在这种情况下,您的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。这样,其他页面可以为不同目的使用相同的重写规则。

相关问题