Not able to rewrite and redirect API URI in Apache htaccess

Viewed 48

I am learning Apache .htaccess configuration. I would like to apply https to a URL if domain is mydomain.com only if URI stars from API and followed by digits Example--> "http://exampledomain.com/api/123456" to be redirected to https://exampledomain.com/index.php?val=123456 in backend server.

I have tried the below but it isn't working. I have not been able to get the API link also to properly rewrite. Any help would be highly appreciated.

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]

1 Answers

Could you please try following, written and tested with shown samples. You were close try to put your https redirection rules first in starting of your Rule file and then proceed with further.

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]
Related