【问题标题】:.htaccess - remove index.php, redirect with multiple parameters.htaccess - 删除 index.php,使用多个参数重定向
【发布时间】:2017-07-03 04:36:59
【问题描述】:

我正在搜索,但我做不到。

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

我一直在尝试多种方法在这里搜索,但我无法完成这项工作。

【问题讨论】:

    标签: php apache .htaccess mod-rewrite parameters


    【解决方案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。这样,其他页面就可以将相同的重写规则用于不同的目的。

    【讨论】:

    • 感谢您的回答。我得到“对象在 Xampp 中没有位置”。我使用 RewriteRule ^([^/.]+)/(.*?)$ /index.php?id=$1&license=$2 [L,QSA] Url 进行测试:localhost/api/source/videos/28652187/23432324
    • index.php 之前删除/。试试这个方法:RewriteRule ^([^/.]+)/(.*?)$ index.php?id=$1&license=$2 [L,QSA]
    • 顺便说一句,我无法测试您的localhost URL :)
    猜你喜欢
    • 2012-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多