Recently I made a small change in my API domain path, I don't want to update my app and I want to solve the URL issue using a .htaccess file.
My current API domain is : https://api.example.com
I need to redirect to https://api.example.com/en/
I have API for
- English -
https://api.example.com/en - Arabic -
https://api.example.com/ar
But my current Englsh API is https://api.example.com only, and I need to add /en/ to it.
If it is /ar/ already then no need to do anything.
I want to redirect to https://api.example.com/en/ if the access domain is only https://api.example.com.
Because before I served the English version of my API directly from the domain but now I've separated that to en - for English and ar for Arabic.
My code :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^api.example.com [NC]
RewriteRule ^(.*)$ https://api.example.com/en/$1 [R=301,L]
Error :
ERR_TOO_MANY_REDIRECTS
In the address bar I can see
http://api.example.com/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/
How I can solve this issue?
CodePudding user response:
You need to check that the requested URL-path does not already start with /en/ or /ar/. For example:
RewriteCond %{HTTP_HOST} ^api\.example\.com [NC]
RewriteCond %{REQUEST_URI} !^/(en|ar)/
RewriteRule ^(.*)$ https://api.example.com/en/$1 [R=301,L]
OR,
RewriteCond %{HTTP_HOST} ^api\.example\.com [NC]
RewriteRule !^(en|ar)/ /en%{REQUEST_URI} [R=301,L]
Aside: Since this is an "API", are requests expecting to have to follow redirects?
