dounuo1881 2016-06-06 12:15
浏览 25

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

Firstly I asked this question on stack to get url rewriting working. I extended this to include a 3rd parameter however I failed when this 3rd parameter is different.

I have created a list of a few urls I currently have with their GET parameters and what I want to turn them into:

~/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

using the following .htaccess:

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]

mostly focusing on the urls regarding /profile/username/edit and /profile/username/overview.

I added in the following rules:

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]

to deal with the &page parameter however when my url rewrites to /profile/username/team/overview I get my edit page back rather than the overview page.

What is the most efficient way to deal with interchanging variables? i.e.

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

Thanks again.

  • 写回答

2条回答 默认 最新

  • doufei16736 2016-06-06 13:13
    关注

    You can use single entry point technique.

    RewriteEngine On
    RewriteBase /tournament/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php [L,QSA]
    

    Now you need to parse the requested URL in index.php to obtain parameters. Something like that:

    $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';
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数