为什么我的 PHP 没有指向我的西班牙网站?

Why isn't my PHP directing to my spanish site?

除了以 "es" 开头的网站外,我的代码适用于我的所有网站。每当我尝试定向到这些网站时,它都会带我回到 index.html

这是我的 index.php 代码:

<?php 
/*
*
License: Creative Commons Attribution 3.0 Unported
License URL: http://creativecommons.org/licenses/by/3.0/
 *
 */
include "app/config.php";
include "app/detect.php";

if ($page_name=='') {
    include $browser_t.'/index.html';
    }
elseif ($page_name=='index.html') {
    include $browser_t.'/index.html';
    }
elseif ($page_name=='es/index.html') {
    include $browser_t.'/es/index.html';
}

else
    {
        include $browser_t.'/404.html';
    }

?>

我正在 linking my site 以便您说出我的意思。在右上角有一个显示 "Español" 的 hyperlink,当您单击它时,它应该会带您到您当前所在页面的西班牙语版本。相反,它只是将您重新带到索引选项卡并更改子目录,但我的翻译页面没有出现。

在 config.php 里面我有以下内容:

<?php
/*
 * 
License: Creative Commons Attribution 3.0 Unported
License URL: http://creativecommons.org/licenses/by/3.0/
 *
 */
$current_page_uri = $_SERVER['REQUEST_URI'];
$part_url = explode("/", $current_page_uri);
$page_name = end($part_url);
$email_id = "support@elitecomps.com";
?>

我会在这里添加我的 detect.php 代码,但它是一个大文件,而不是我试图解决的问题,所以我只会 link 它。

PS 一切都 link 适当地编辑,我已经三重检查了一切。我唯一怀疑的是我的 index.php,我不确定为什么。

$part_url = explode("/", $current_page_uri);
$page_name = end($part_url);

当 URL 为 "es/index.html" 时,该代码会将 $page_name 分配给 "index.html",因为您只对最后一个 [=13] 之后的内容感兴趣=].

一种方法是使用 strrpos 检查字符串的结尾,例如

if(strrpos("es/index.html", $current_page_uri) !== FALSE)
    // Go to spanish site
else if(strrpos("index.html", $current_page_uri) !== FALSE)
    // Normal index page

您还可以检查 $part_url 中的倒数第二个元素并检查它是否是 "es" 或任何其他语言代码。

如果我的逻辑正确,你应该在这里使用另一个条件

替换:

if ($page_name=='') {
    include $browser_t.'/index.html';
    }
elseif ($page_name=='index.html') {
    include $browser_t.'/index.html';
    }
elseif ($page_name=='es/index.html') {
    include $browser_t.'/es/index.html';
}

else
    {
        include $browser_t.'/404.html';
    }

if ($current_page_uri=='') {
    include $browser_t.'/index.html';
    }
elseif ($current_page_uri=='index.html') {
    include $browser_t.'/index.html';
    }
elseif ($current_page_uri=='es/index.html') {
    include $browser_t.'/es/index.html';
}

else
    {
        include $browser_t.'/404.html';
    }

我认为您打算使用 $page_name 作为检测器。但是您尝试比较的值 es/index.html 实际上是您的 uri。