是否可以设置一个基本的HTML页面以在加载时重定向到另一个页面?
当前回答
你应该使用JavaScript。将以下代码放在头标签中:
<script type="text/javascript">
window.location.assign("http://www.example.com")
</script>
其他回答
你应该使用JavaScript。将以下代码放在头标签中:
<script type="text/javascript">
window.location.assign("http://www.example.com")
</script>
这不需要任何JavaScript代码。将其写入HTML页面的<head>部分:
<meta http-equiv="refresh" content="0; url=example.com" />
页面在0秒加载后,即可转到页面。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Redirect to a page</title>
</head>
<body onload="window.location.assign('http://example.com')">
</body>
</html>
只需使用body标签的onload事件:
<body onload="window.location = 'http://example.com/'">
我将同时使用元代码和JavaScript代码,并将有一个链接以防万一。
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta http-equiv="refresh" content="0; url=http://example.com">
<script type="text/javascript">
window.location.href = "http://example.com"
</script>
<title>Page Redirection</title>
</head>
<body>
<!-- Note: don't tell people to `click` the link, just tell them that it is a link. -->
If you are not redirected automatically, follow this <a href='http://example.com'>link to example</a>.
</body>
</html>
为了完整起见,如果可能的话,我认为最好的方法是使用服务器重定向,所以发送301状态代码。这可以通过使用Apache的.htaccess文件或使用WordPress的众多插件轻松实现。我确信所有主要的内容管理系统都有插件。此外,如果您的服务器上安装了301重定向,cPanel可以非常轻松地配置301重定向。