【问题标题】:apache mod_rewrite - Converting clean url to query string url with variable number of parametersapache mod_rewrite - 将干净的 url 转换为具有可变参数数量的查询字符串 url
【发布时间】:2019-04-12 10:29:57
【问题描述】:

在我的应用程序中使用以下 .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重写?

【问题讨论】:

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


    【解决方案1】:

    我刚刚在我的应用程序的 /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);
        }
    

    编辑:添加第一个条件块会产生更正确的输出,请参阅 cmets

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

    【讨论】:

      猜你喜欢
      • 2012-08-26
      • 2016-12-21
      • 2013-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-27
      • 2018-11-29
      • 1970-01-01
      相关资源
      最近更新 更多