Using the following .htaccess in my application (adapted from 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
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/
I only get to step 4 below
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
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/
<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
{
"key1": "val1",
"key2": "val2",
"key3": "val3",
"key4": "val4"
}
api.php script
<?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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.