簡體   English   中英

apache mod_rewrite-將純凈網址轉換為帶有可變數量參數的查詢字符串網址

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

在我的應用程序中使用以下.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

我想轉換

/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

如何完成網址重寫?

我剛剛在應用程序的/ 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM