我有以下.htaccess文件。
<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 %{QUERY_STRING} ^$
RewriteRule ^/([^/]+)/([^/]+)$ $1/$1.php?endpoint=$2 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ $1/$1.php?endpoint=$2&id=$3 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)$ $1/$1.php?endpoint=$2&id=$3&endpoint2=$4 [L]
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^([^/]+)$ $1/$1.php [L]
RewriteRule ^([^/]+)/([^/]+)? $1/$1.php?endpoint=$2%1 [QSA,L]
RewriteRule ^/([^/]+)/([^/]+)/([^/]+)? $1/$1.php?endpoint=$2&id=$3%1 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)? $1/$1.php?endpoint=$2&id=$3&endpoint2=$4%1 [QSA,L]
</IfModule>
我想将api端点(干净的url格式与最后一个令牌末尾的可能查询)重写为完整的查询字符串格式,如下所示。
例
api/users/123/actionitems
目前读
api/api.php?endpoint=users&id=123&endpoint2=actionitems
{
endpoint: users,
id: 123,
endpoint2: actionitems
}
但我也想转换
api/users/123/actionitems?test=3
成
api/api.php?endpoint=users&id=123&endpoint2=actionitems&test=3
{
endpoint: users,
id: 123,
endpoint2: actionitems,
test: 3
}
它不起作用。我只得到
api/api.php?endpoint=users&id=123&endpoint2=actionitems
当我输入
/api/users/123/actionitems?test=3
{
endpoint: users,
id: 123,
endpoint2: actionitems
}
只有当我在我的请求中键入/api/users/123/actionitems&test=3
而不是问号(/api/users/123/actionitems?test=3
)时才有效。
我如何让它工作?
您可以使用以下单个规则:
Options +FollowSymlinks
RewriteEngine On
DirectoryIndex api.php
FallbackResource index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^api/([^/]+)/([^/]+)/([^/]+)/?$ /api/api.php?endpoint=$1&id=$2&endpoint2=$3 [QSA,L]
工作规则(到目前为止......)
<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 %{QUERY_STRING} ^(.*)$
RewriteRule ^api/$ /api/api.php? [L]
RewriteRule ^api/([^/]+)/$ /api/api.php?endpoint=$1%1 [QSA,L]
RewriteRule ^api/([^/]+)/([^/]+)$ /api/api.php?endpoint=$1&id=$2%1 [QSA,L]
RewriteRule ^api/([^/]+)/([^/]+)/([^/]+)$ /api/api.php?endpoint=$1&id=$2&endpoint2=$3%1 [QSA,L]
#Disgard tokens after 3rd token
RewriteRule ^api/([^/]+)/([^/]+)/([^/]+)/(.*)$ /api/api.php?endpoint=$1&id=$2&endpoint2=$3%1 [QSA,L]
</IfModule>