对于语言重定向,我们当前在Web根目录中创建包含index.php文件的文件夹,该文件检查HTTP_ACCEPT_LANGUAGE服务器变量.例如,为网址www.example.com/press/
在/var/www/site/press/index.php:
<?php
if ($_SERVER["HTTP_ACCEPT_LANGUAGE"] == "en")
header("location: ../press_en.php");
else
header("location: ../press_de.php");
?>
Run Code Online (Sandbox Code Playgroud)
随着网站的增长,我们现在有很多这样的文件夹.我试图通过将重定向移动到单个.htaccess文件来清理它:
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]
Run Code Online (Sandbox Code Playgroud)
这个想法与php文件相同,但它不起作用.我在Firefox首选项中尝试了所有可能的语言设置/命令,并检查了标题是否正确,但它始终为press_de.php文件提供服务.
我做错了什么,或者有更好的方法吗?(不包括内容协商/多视图或任何需要重命名文件的内容,目前这不是一个选项).