apache mod_rewrite-将纯净网址转换为参数数量可变的查询字符串网址

时间:2019-04-11 04:09:03

标签: apache .htaccess mod-rewrite url-rewriting

在我的应用程序中使用以下.htaccess(改编自apache mod_rewrite one rule for any number of possibilities

RewriteCond %{REQUEST_URI} ^/api
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^([^/]+)/([^/]+)/(.*) $1?$2=$3
RewriteCond %{QUERY_STRING} (.*) 
RewriteRule ^([^/]+)/ $1/$1.php?%1

我要转换

/api/test/1/2/3/4/5        (for any number of params as only 6 are shown after /api)

进入

api/api.php?test=1&2=3&4=5    (for any number of params)

但是,使用https://htaccess.madewithlove.be/

我只能执行下面的第4步

1   RewriteCond %{REQUEST_URI} ^/api                 This condition was met.
2   RewriteCond %{REQUEST_FILENAME} !-d              This condition was met.
3   RewriteCond %{REQUEST_FILENAME} !-f              This condition was met.
4   RewriteRule ^([^/]+)/([^/]+)/(.*) $1?$2=$3       The new url is :80/api?test=1/2/3/4/5
5   RewriteCond %{QUERY_STRING} (.*)                 This rule was not met.
6   RewriteRule ^([^/]+)/ $1/$1.php?%1               This rule was not met

如何完成url重写?

1 个答案:

答案 0 :(得分:0)

我刚刚在我的应用程序/ api目录的根目录中修改了我的.htaccess文件

通过删除问题中未显示的原始代码

RewriteBase /api
RewriteOptions InheritDown

在api /

之后,我只剩下处理偶数个令牌的以下内容
<IfModule mod_rewrite.c> 
    Options +FollowSymlinks
    RewriteEngine On
    DirectoryIndex api.php
    FallbackResource index.php
    RewriteCond %{REQUEST_URI} ^/api
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} ^/api
    #RewriteRule (.*) $1
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{QUERY_STRING} ^(.*)$
    RewriteRule ^(.*/)([^/]+)/([^/]+) $1?$2=$3&%1 [QSA,L]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{QUERY_STRING} ^(.*)$
    RewriteRule ^([^/]+)/ $1/$1.php?%1 [L]
</IfModule>


http://<host>/api/key1/val1/key2/val2/key3/val3/key4/val4

现在在我的php脚本api.php中产生一个json(输出键值对)

    {
        "key1": "val1",
        "key2": "val2",
        "key3": "val3",
        "key4": "val4"
    }

api.php脚本

<?php
    $requestURI = $_GET;

    $method = strtolower($_SERVER['REQUEST_METHOD']);
    switch ($method){
        case 'get':
             //var_dump($_GET);
             echo json_encode($requestURI);
            break;
        case 'post':
            echo json_encode($requestURI);
            break;
        case 'put':
            echo json_encode($requestURI);
            break;
        case 'delete':
            echo json_encode($requestURI);
            break;
        default:
            // unimplemented method
            http_response_code(405);
    }

编辑:添加第一个条件块会导致输出更正确,请参见注释

<IfModule mod_rewrite.c> 
    Options +FollowSymlinks
    RewriteEngine On
    DirectoryIndex api.php
    FallbackResource index.php

    #Redirects all existing files to the containing directory which
    #then get handled by the 403 error document in .htaccess in /api folder
    #but two token params get still rewritten as /?token1=token2
    RewriteCond %{REQUEST_URI} ^/api
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule  (.*/)([^/]+).php$ /$1 [R=302,L]

    #Non exisitng resoruce then rewrite all items with one token to {'endpoint': <endpoint>}
    RewriteCond %{REQUEST_URI} ^/api
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{QUERY_STRING} ^$
    RewriteRule ^api/([^/]+) api/endpoint/$1

    #Rewrite non exisitng request to append query string to construct the query param form starting with question mark
    RewriteCond %{REQUEST_URI} ^/api
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{QUERY_STRING} ^(.*)$
    RewriteRule ^(.*/)([^/]+)/([^/]+) $1?$2=$3&%1 [QSA,L]

    #Append query string to end of question mark and write /api as api/api.php?token....
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{QUERY_STRING} ^(.*)$
    RewriteRule ^([^/]+)/ $1/$1.php?%1 [L]    
</IfModule>