现在,我正在使用自定义 PHP 解决方案来强制将 WWW 附加到我的脚本 URL,因为我使用 WordPress 的 .htaccess 规则来“清理” index.php。强制 WWW .htaccess 规则与 WordPress 的 index.php 清理规则不兼容。
使用我的脚本,您可以浏览到“http://scripturl.com/whatever”——然后我的脚本有一个请求 URI“/whatever”的案例,然后为该视图执行一些操作(或编译相应的模板)。
在调用任何开关之前,我添加了一系列检查,以确保来自 REQUEST 的 HTTP 主机与来自我脚本中定义的变量的 HTTP 主机匹配。这会强制添加“www”。
我的问题 - 是(似乎只使用 IE 时) - 当我输入一个 url 时,说“http://myscript.com/whatever”,我的脚本将 url 转换为“http://www.myscript.com/whatever ",正如预期的那样,然后将标头重定向到新的 URL。但是,如果我将 URI 请求从“whatever”更改为“somethingelse”,页面会按预期转到“http://www.myscript.com/somethingelse”,但在短时间内,“whatever”会在 url 中闪烁在脚本重定向到“http://www.myscript.com/somethingelse”之前。
澄清:从请求“www.myscript.com/sam”开始。请求负载。将 /sam 更改为 /bob -> 页面更改为“www.myscript.com/bob”,但在 /bob 加载之前,“/sam”会在 url 栏中短暂闪烁。
它只是感觉不“干净”。我觉得我的代码可能会进行额外的标题跳转或其他操作。我将其与 wordpress 进行了对比,转到“www.wordpressurl.com/valid-page”,然后将 URI 更改为“www.wordpressurl.com/another-valid-page” - 我没有看到“/valid-页面”在尝试访问“/another-valid-page”时在 URL 栏中闪烁,反之亦然。
这是我的代码:
// Requested URL built from url in address bar
$requested_url = is_ssl() ? 'https://' : 'http://';
$requested_url .= $_SERVER['HTTP_HOST'];
$requested_url .= $_SERVER['REQUEST_URI'];
// Correct url built from predefined variable
$correct_url = is_ssl() ? 'https://' : 'http://';
// "Correct" script url
$user_home = @parse_url('http://www.myscript.com');
if ( !empty($user_home['host']) )
$correct_url .= $user_home['host'];
else {
die('malformed url');
}
$correct_url .= $_SERVER['REQUEST_URI'];
// If URL in address bar is not proper, perform redirect (preserve URI)
if ($correct_url != $requested_url) {
hc_redirect($correct_url, 301);
}
// Get page from request, handle accordingly
$page = $_SERVER['REQUEST_URI'];
switch ($page) {
/* Index */
case '/':
echo "Index queried <br />";
break;
...
为什么在加载新 URI(来自重定向)之前旧 URI 在导航栏中闪烁?就像我说的,这似乎只发生在 IE 中 - 但 WordPress 没有这种相同的行为(在 IE 或任何其他浏览器中),所以我知道我的代码肯定有问题,这是一个“额外的步骤”,没有发生我的知识。我对 PHP 有点陌生。
有什么想法吗?
编辑:hc_redirect 和其他使用的函数:http ://pastebin.com/fVNEckEg