【发布时间】:2012-07-11 20:41:40
【问题描述】:
我在http://site.com/services/ 有一个页面,我只想将其重定向到http://site.com/servics/first-service/
执行此操作的最佳重定向是什么以及如何执行此操作?
【问题讨论】:
我在http://site.com/services/ 有一个页面,我只想将其重定向到http://site.com/servics/first-service/
执行此操作的最佳重定向是什么以及如何执行此操作?
【问题讨论】:
PHP:
header("Location: yourpage.php");
exit;
您必须在header 之后使用exit 或die 才能停止执行您的代码。
此外,代码必须在任何输出之前执行。
HTML:
<meta http-equiv="refresh" content="0;url=yourpage.php">
代码应该放在<head></head>标签之间。
JavaScript:
window.location = "yourpage.php";
.htaccess:
Redirect 301 /oldpage.php /newpage.php
【讨论】:
exit() 或 die() - 如果您不使用其中之一,则重定向完成后其余代码将继续执行。
yourpage.php,不仅是那些在/services 路径上找到的页面,第二段代码依赖于JavaScript,并且被认为非常不受欢迎,因为网络爬虫确实倾向于考虑客户端诱导刷新是恶意的。
header 将重定向任何页面。为什么有人会将其放在不应重定向的页面上?把它放在你想要重定向用户的页面上。
这可以通过一个简单的Redirect 指令来完成。
Redirect /services /services/first-service
您可以选择发送给客户的响应代码类型。有关我提供的链接的更多信息。
【讨论】:
如果是永久更改,要使用301重定向google有一些good content on the matter
要构建重定向,只需添加
redirect 301 /services/ http://site.com/services/first-service/
到目录树顶层的 .htacess。 注意我将“servcs”从您的示例切换到服务
【讨论】:
在javascript中:window.location = "/first-service";
【讨论】:
我将其用作文件夹中的索引文件,我不希望公众浏览,,,,
<meta name="robots" content="noindex, nofollow" />
<meta name="googlebot" content="noindex, nofollow" />
<?php
$url = 'http://' . $_SERVER['HTTP_HOST']; // Get the server//
header('Location: ' . $url, true, 301); // Use either 301 or 302//
?>
【讨论】:
你试过 javascript window.location = "http://site.com/servics/first-service/"; 吗?我希望它有效。
【讨论】:
window.location 不是函数。
location window 对象的属性。 Read more here。 window.location('') 不正确。正确的是window.location = ''