首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >同一级别不同get参数的htaccess重写

同一级别不同get参数的htaccess重写
EN

Stack Overflow用户
提问于 2016-06-06 20:16:00
回答 2查看 117关注 0票数 0

首先,我问了这个问题on stack让url重写工作。我将其扩展为包括第三个参数,但是当第三个参数不同时,我失败了。

我已经创建了一个列表,列出了我当前拥有的几个urls及其GET参数,以及我想要将它们转换成的内容:

代码语言:javascript
复制
~/tournament/profile/username           /tournament/index.php?view=profile&id=username
~/tournament/profile/username/edit      /tournament/index.php?view=profile&id=username&action=edit
~/tournament/profile/username/overview  /tournament/index.php?view=profile&id=username&page=overview
~/tournament/profile/username/teams     /tournament/index.php?view=profile&id=username&page=teams


~/tournament/teams/create               /tournament/index.php?view=teams&action=create
~/tournament/teams/teamname             /tournament/index.php?view=teams&id=teamname
~/tournament/teams/teamname/edit        /tournament/index.php?view=teams&id=teamname&action=edit
~/tournament/teams/teamname/delete      /tournament/index.php?view=teams&id=teamname&action=delete
~/tournament/teams/teamname/members     /tournament/index.php?view=teams&id=teamname&page=members

~/tournament/cups                       /tournament/index.php?view=cups
~/tournament/cups/id                    /tournament/index.php?view=cups&id=cupid
~/tournament/cups/id/rules              /tournament/index.php?view=cups&page=rules
~/tournament/cups/id/matches            /tournament/index.php?view=cups&page=matches
~/tournament/cups/id/brackets           /tournament/index.php?view=cups&page=brackets
~/tournament/cups/id/teams              /tournament/index.php?view=cups&page=teams

~/tournament/passwords/forgot_password  /tournament/index.php?view=password&action=forgot_password
~/tournament/passwords/reset            /tournament/index.php?view=password&action=reset

~/tournament/admin                      /tournament/admin.php
~/tournament/admin/users                /tournament/admin.php?view=users
~/tournament/admin/users/username       /tournament/admin.php?view=users&id=username

使用以下.htaccess:

代码语言:javascript
复制
RewriteEngine On
RewriteBase /tournament/

# Skip if existing file/folder
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteCond %{THE_REQUEST} \s/tournament/index\.php\?view=([^&\s]+)\s [NC]
RewriteRule ^ %1? [R=301,L]
RewriteCond %{THE_REQUEST} \s/tournament/index\.php\?view=([^&\s]+)&id=([^&\s]+)\s [NC]
RewriteRule ^ %1/%2? [R=301,L]
RewriteCond %{THE_REQUEST} \s/tournament/index\.php\?view=([^&\s]+)&id=([^&\s]+)&action=([^&\s]+)\s [NC]
RewriteRule ^ %1/%2/$3? [R=301,L]
RewriteCond %{THE_REQUEST} \s/tournament/index\.php\?view=([^&\s]+)&id=([^&\s]+)&page=([^&\s]+)\s [NC]
RewriteRule ^ %1/%2/$3? [R=301,L]


RewriteRule ^([^/]+)$ index.php?view=$1 [L]
RewriteRule ^([^/]+)/([^/]+)$ index.php?view=$1&id=$2 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?view=$1&id=$2&action=$3 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?view=$1&id=$2&page=$3 [L]

主要关注关于/profile/username/edit/profile/username/overview的urls。

我添加了以下规则:

代码语言:javascript
复制
RewriteCond %{THE_REQUEST} \s/tournament/index\.php\?view=([^&\s]+)&id=([^&\s]+)&page=([^&\s]+)\s [NC]
RewriteRule ^ %1/%2/$3? [R=301,L]

RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?view=$1&id=$2&page=$3 [L]

然而,为了处理&page参数,当我的url重写为/profile/username/team/overview时,我得到的是编辑页面,而不是概述页面。

处理互换变量的最有效方法是什么?即

?view=*&id=*&action=* vs ?view=*&id=*&page=*

再次感谢。

EN

回答 2

Stack Overflow用户

发布于 2016-06-06 21:13:34

您可以使用单入口点技术。

代码语言:javascript
复制
RewriteEngine On
RewriteBase /tournament/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L,QSA]

现在您需要在index.php中解析请求的URL以获取参数。大概是这样的:

代码语言:javascript
复制
$view = 'default';
$id = '0';
$params = array();

if ($_SERVER['REQUEST_URI'] != '/') {
    try {
        $url_path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
        $uri_parts = explode('/', trim($url_path, ' /'));

        if (count($uri_parts) % 2) {
            throw new Exception();
        }

        $view = array_shift($uri_parts); 
        $id = array_shift($uri_parts);

        // /key1/value1/key2/value2 -> array()
        for ($i=0; $i < count($uri_parts); $i++) {
            $params[$uri_parts[$i]] = $uri_parts[++$i];
        }
    } catch (Exception $e) {
        $module = '404';
        $action = 'main';
    }
}
票数 0
EN

Stack Overflow用户

发布于 2016-06-06 21:22:58

您可以使用:

代码语言:javascript
复制
RewriteEngine on
#redirect from "/tournament/index.php?view=foo&id=bar&action=edit" to "/tournament/foo/bar/edit"
RewriteCond %{THE_REQUEST} /tournament/index.php\?view=([^&]+)&id=([^&]+)&action=(edit)\sHTTP [NC]
RewriteRule ^ /tournament/%1/%2/%3? [L,R]
#redirect from "/tournament/index.php?view=foo&id=bar" to "/tournament/foo/bar/"
RewriteCond %{THE_REQUEST} /tournament/index.php\?view=([^&]+)&id=([^&]+)\sHT [NC]
RewriteRule ^ /tournament/%1/%2? [L,R]
#redirect from "/tournament/index.php?view=foo" to "/tournament/foo"
RewriteCond %{THE_REQUEST} /tournament/index.php\?view=([^&]+)\sHTTP [NC]
RewriteRule ^ /tournament/%1? [L,R]
#internally rewrite external requests to orignal location
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/?([^/]*)/?(.*?)/?$ /tournament/index.php?view=$1&id=$2&page=$3 [NC,L]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37657215

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档