自动重定向到另一个 HTML 页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27895424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Auto-redirect to another HTML page
提问by Hingle Mcringleberry
What is the syntax for making a page auto-redirect to a different HTML file in a separate folder? All of my searching returns how to redirect from one website to another.
使页面自动重定向到单独文件夹中不同 HTML 文件的语法是什么?我所有的搜索都会返回如何从一个网站重定向到另一个网站。
回答by Prashant Shastri
One of these will work...
其中之一将工作...
<head>
<meta http-equiv='refresh' content='0; URL=http://example.com/'>
</head>
...or it can done with JavaScript:
...或者它可以用 JavaScript 完成:
window.location.href = 'https://example.com/';
回答by Rafael
<meta http-equiv="refresh" content="5; url=http://example.com/">
<meta http-equiv="refresh" content="5; url=http://example.com/">
回答by Ding
If you're using Apache and can use a .htaccess
file you should use the following type of redirect. Add the following to an .htaccess
file in the root of your website.
如果您使用 Apache 并且可以使用.htaccess
文件,则应使用以下类型的重定向。将以下内容添加到.htaccess
网站根目录中的文件中。
RewriteEngine On
RewriteRule ^/oldfile_path/file_name\.html$ /oldfile_path/file_name.html [R=301,L]
This has the advantage of being a very fast and immediate redirect. It also depends on your reason for the redirect. This is a more permanent method because it sends the HTTP 301 status code signifying that the file has moved permanently and causes many browsers to cache that request. You can change the code to something else like a 302 for temporary redirects.
这具有非常快速和直接重定向的优点。这也取决于您重定向的原因。这是一种更持久的方法,因为它发送 HTTP 301 状态代码,表示文件已永久移动并导致许多浏览器缓存该请求。您可以将代码更改为其他类似 302 的代码以进行临时重定向。
Otherwise you can do a simple redirect using an HTML <meta>
tag as suggested by others:
否则,您可以<meta>
按照其他人的建议使用 HTML标签进行简单的重定向:
<meta http-equiv="refresh" content="5; url=http://example.com/">
By default the content="5"
makes that redirect after 5 seconds. This will be slower and not all browsers support it. A redirect can also be done in the server language of your choice PHP
, Node.js
, etc.
默认情况下,content="5"
会在 5 秒后进行重定向。这会更慢,而且并非所有浏览器都支持它。一个重定向也可以在您所选择的服务器语言完成PHP
,Node.js
等等。
回答by Girish
You can use <meta>
tag refresh, and <meta>
tag in <head>
section
您可以使用<meta>
标签刷新,并<meta>
在<head>
部分中使用标签
<META http-equiv="refresh" content="5;URL=your_url">
回答by Sudiste
If you want to redirect your webpage to another HTML FILE, just use as followed:
如果您想将您的网页重定向到另一个 HTML 文件,只需使用如下:
<meta http-equiv="refresh" content"2;otherpage.html">
2 being the seconds you want the client to wait before redirecting. Use "url="
only when it's an URL, to redirect to an HTML file just write the name after the ';'
2 是您希望客户端在重定向前等待的秒数。"url="
仅当它是 URL 时使用,重定向到 HTML 文件只需在';'
回答by AKNair
Its a late answer, but as I can see most of the people mentioned about "refresh" method to redirect a webpage. As per W3C, we should not use "refresh" to redirect. Because it could break the "back" button. Imagine that the user presses the "back" button, the refresh would work again, and the user would bounce forward. The user will most likely get very annoyed, and close the window, which is probably not what you, as the author of this page, want.
这是一个迟到的答案,但正如我所看到的,大多数人都提到了重定向网页的“刷新”方法。根据 W3C,我们不应该使用“刷新”来重定向。因为它可能会破坏“后退”按钮。想象一下,用户按下“后退”按钮,刷新会再次起作用,用户会向前弹跳。用户很可能会非常恼火,并关闭窗口,这可能不是您作为此页面的作者想要的。
Use HTTP redirects instead. One can refer the complete documentation here: W3C document
请改用 HTTP 重定向。可以在这里参考完整的文档:W3C 文档