简体   繁体   English

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

[英]apache mod_rewrite - Converting clean url to query string url with variable number of parameters

Using the following .htaccess in my application (adapted from apache mod_rewrite one rule for any number of possibilities ) 在我的应用程序中使用以下.htaccess(适用于apache mod_rewrite一种规则,适用于多种可能性

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

I want to convert 我想转换

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

into 进入

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

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

I only get to step 4 below 我只会进入下面的第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

How do I complete the url rewrite? 如何完成网址重写?

I just modified my .htaccess file in question in the root of /api directory in my app 我刚刚在应用程序的/ api目录的根目录中修改了我的.htaccess文件

by removing in my original code not shown in my question 通过删除我的问题中未显示的原始代码

RewriteBase /api
RewriteOptions InheritDown

I am left with the following that handles only even number of tokens after api/ 我剩下的以下内容仅处理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

Now Produces a json (outputting the key value pairs) in my php script api.php 现在在我的php脚本api.php中产生一个json(输出键值对)

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

api.php script 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);
    }

EDIT: Addition of the first condition block resulted in a more correct output, see comments 编辑:添加第一个条件块导致更正确的输出,请参见注释

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM