For language redirects we currently create folders in the web root containing an index.php file which checks the HTTP_ACCEPT_LANG
server variable. e.g. for the url www.example.com/press/
in /var/www/site/press/index.php
:
<?php
if ($_SERVER["HTTP_ACCEPT_LANGUAGE"] == "en")
header("location: ../press_en.php");
else
header("location: ../press_de.php");
?>
As the site has grown, we now have many such folders. I am trying to clean this up by moving the redirects to a single .htaccess file:
RewriteEngine on
# Set the base path here
RewriteBase /path/to/site/
# The 'Accept-Language' header starts with 'en'
RewriteCond %{HTTP:Accept-Language} (^en) [NC]
# EN redirects
RewriteRule press(/?)$ press_en.php [L,R]
# DE redirects (for all languages not EN)
RewriteRule press(/?)$ press_de.php [L,R]
The idea is the same as the php file, but it doesn't work. I have tried all the possible language settings / orders in Firefox preferences, and checked the headers are correct, but it always serves the press_de.php
file.
What am I doing wrong, or is there a better way? (not including content negotiation / multiviews or anything that requires renaming files, this is not currently an option).