Auto-redirect to another HTML page

前端 未结 7 546
眼角桃花
眼角桃花 2020-12-13 12:58

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.

相关标签:
7条回答
  • 2020-12-13 13:32

    If you want to redirect your webpage to another HTML FILE, just use as followed:

    <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 ';'

    0 讨论(0)
  • 2020-12-13 13:35

    <meta http-equiv="refresh" content="5; url=http://example.com/">

    0 讨论(0)
  • 2020-12-13 13:37

    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.

    Use HTTP redirects instead. One can refer the complete documentation here: W3C document

    0 讨论(0)
  • 2020-12-13 13:44

    You can use <meta> tag refresh, and <meta> tag in <head> section

    <META http-equiv="refresh" content="5;URL=your_url"> 
    
    0 讨论(0)
  • 2020-12-13 13:48

    One of these will work...

    <head>
      <meta http-equiv='refresh' content='0; URL=http://example.com/'>
    </head>

    ...or it can done with JavaScript:

    window.location.href = 'https://example.com/';

    0 讨论(0)
  • 2020-12-13 13:50

    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.

    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.

    Otherwise you can do a simple redirect using an HTML <meta> tag as suggested by others:

    <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.

    0 讨论(0)
提交回复
热议问题