无法在Apache htaccess中重写和重定向API URI



我正在学习Apache .htaccess配置。我想将https应用到URL,如果域名是mydomain.com如果URI来自API,后面跟着数字例子——比;"http://exampledomain.com/api/123456"在后台服务器上重定向到https://exampledomain.com/index.php?val=123456。

我试过下面的,但它不工作。我还没有能够得到API链接也正确重写。如有任何帮助,我将不胜感激。

RewriteEngine On
RewriteRule ^api/([0-9]+) index.php?val=$1 [NC,L]
RewriteCond %{HTTP_HOST} ~exampledomain.com [NC]
RewriteRule (.*) https://%{HTTP_HOST}$1 [R,NC]

请尝试以下,书面和测试显示的样品。请先将https重定向规则放在规则文件的开头,然后再继续。

RewriteEngine ON
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(exampledomain.com) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,NE,L]
RewriteRule ^api/(d+)/?$ index.php?val=$1 [NC,L]

最新更新