GET参数通过php未读取的.htaccess子域传递 脚本
我有以下代码: test.php 我的代码应该做什么 如果未定义$_GET['lang'],则从从浏览器发送的标题中获取用户的语言,并将其重定向到与其语言对应的子域:子域应与$_GET['lang']一致 什么不起作用 通过访问localhost/test.php,我被重定向到正确的子域,但重定向循环无休止。 另外,如果我访问en.localhost/test.php并且我的语言是意大利语,那么在循环之前我会被重定向到it.localhost/test.php 我的问题 如何解决此问题?您可以使用:GET参数通过php未读取的.htaccess子域传递 脚本,php,apache,.htaccess,mod-rewrite,redirect,Php,Apache,.htaccess,Mod Rewrite,Redirect,我有以下代码: test.php 我的代码应该做什么 如果未定义$_GET['lang'],则从从浏览器发送的标题中获取用户的语言,并将其重定向到与其语言对应的子域:子域应与$_GET['lang']一致 什么不起作用 通过访问localhost/test.php,我被重定向到正确的子域,但重定向循环无休止。 另外,如果我访问en.localhost/test.php并且我的语言是意大利语,那么在循环之前我会被重定向到it.localhost/test.php 我的问题 如何解决此问题?您可以使
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP:Accept-Language} ^([a-z]{2}) [NC]
RewriteRule ^ - [E=LANG:%1]
RewriteCond %{QUERY_STRING} !(?:^|&)lang= [NC]
RewriteCond %{HTTP_HOST} ^(?:[^.]+\.)?(localhost)$
RewriteRule (.*) http://%{ENV:LANG}.%1/$1?lang=%{ENV:LANG} [QSA,L,R=302]
Test.php:
<?php
$lang = $_GET['lang'];
$content = array("en"=>"This is a test.","it"=>"Questo è un test.");
echo $content[$lang];
?>
感谢您的回答,但不幸的是,这不起作用:当我访问localhost/test.php时,我只被重定向到it.localhost/test.php一次,而当我访问en.localhost/test.php时,页面以意大利语显示。请参见更新的答案。另请参见建议的Test.php
,它不应该做任何重定向,因为它现在正在重写规则中处理。再次感谢,如果我访问localhost/Test.php,这是可行的,但是,如果我转到en.localhost/Test.php或it.localhost/Test.php,我会得到“未定义的索引:lang”
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP:Accept-Language} ^([a-z]{2}) [NC]
RewriteRule ^ - [E=LANG:%1]
RewriteCond %{QUERY_STRING} !(?:^|&)lang= [NC]
RewriteCond %{HTTP_HOST} ^(?:[^.]+\.)?(localhost)$
RewriteRule (.*) http://%{ENV:LANG}.%1/$1?lang=%{ENV:LANG} [QSA,L,R=302]
<?php
$lang = $_GET['lang'];
$content = array("en"=>"This is a test.","it"=>"Questo è un test.");
echo $content[$lang];
?>