Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

.htaccess - Not able to rewrite and redirect API URI in Apache htaccess

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]

question from:https://stackoverflow.com/questions/65869511/not-able-to-rewrite-and-redirect-api-uri-in-apache-htaccess

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...