RS39200
RS39200

Reputation: 13

Htaccess redirection not working, server error

I am facing a problem 500 server error, I try to redirect the user to a different URL depending on his country of origin using the .htaccess file and GEOIP, I use Codeigniter 3.

Error 500 : Is [Fri Jul 15 15:37:32.432374 2022] [core:error] [pid 3029:tid 140071902476032] [client 86.235.179.208:0] AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.

I would like to do this : https://example.com ----> https://example.com/fr/

Here's my script before adding the code :


    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]

and after adding the code :


    RewriteEngine On
    RewriteCond %{HTTP:X-FORWARDED-COUNTRY} ^France$
    RewriteRule ^([^/]+)$ /fr/ [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]

Thanks in advance for your help.

Upvotes: 0

Views: 259

Answers (1)

arkascha
arkascha

Reputation: 42984

The comments suggest that you are actually trying to redirect requests in a country specific manner. That is not what you implemented, though.

Take a look at this slightly modified variant:

RewriteEngine On

RewriteCond %{HTTP:X-FORWARDED-COUNTRY} ^France$
RewriteCond %{REQUEST_URI} !^/fr/
RewriteRule ^ /fr%{REQUEST_URI} [R=301,END]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.php?%{REQUEST_URI} [L]

The modifications should actually externally redirect requests without the /fr/ prefix if applicable. The rewriting loops is prevented because of the additional condition preventing the redirection if it already has been applied in a previous round trip. Not that I did not test this, I just sketched it here. You may have to adjust it to your specific setup.

Remember to always test using a fresh anonymous browser window. Also using a R=302 temporary redirection is a good idea and to only change that to a R=301 permanent redirection once your are satisfied. That prevents nasty caching issues on the client side.

You can implement such rules in the http server's host configuration. If you do not have access to that you can also use a distributed configuration file (".htaccess") which should be located in the DOCUMENT_ROOT folder of your http host.

Upvotes: 0

Related Questions