我编写了一个宁静的api,它使用以下apache mod重写规则:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /api
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ https://example.com/api/index.php?request=$1 [L]
</IfModule>
但是,在控制器中,我发现我无法正确获取查询字符串。
当我尝试向https://example.com/api/search/article?mh=7发送请求后获取查询字符串时:(1)
$_SERVER['QUERY_STRING']
我得到以下结果:
request=search/article
当我尝试以下操作时:(2)
$_SERVER['REQUEST_URI']
我得到以下结果:
/api/index.php?request=search/article
我该怎么做以适当地使用mod重写并在我的api控制器中成功获取查询字符串?
注意:示例中的实际网址已更改。
假设您的htaccess位于/api/
文件夹中,它应该看起来像这样
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /api/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?request=$1 [L,QSA]
</IfModule>
注意:请勿http://
在您的规则中使用,否则将是外部重定向。另外,请使用QSA
标志来附加来自初始url的查询字符串。
从您的示例:https://example.com/api/search/article?mh=7
将被重写为/api/index.php?request=search/article&mh=7
编辑:如果您希望http
您的API的每个URL重定向到https
等效
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /api/
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/api/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?request=$1 [L,QSA]
</IfModule>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句