javascript HTML 页面重定向到脚本的最快方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7225489/
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
Fastest way for a HTML page to redirect to a script
提问by Georman
I am looking for fast methods/ways I can redirect a HTML page to another page.
我正在寻找可以将 HTML 页面重定向到另一个页面的快速方法/方式。
Currently I use meta-tags to redirect a HTML page to a python script that grabs the website HTML from a SQL database(grabs different HTML based on whether user is on a Mobile device) then sends that HTML back. But this is visibly slow, it shows a blank white page(redirect HTML page) then shows the python script page.
目前,我使用元标记将 HTML 页面重定向到 Python 脚本,该脚本从 SQL 数据库中获取网站 HTML(根据用户是否使用移动设备获取不同的 HTML),然后将该 HTML 发回。但这显然很慢,它显示一个空白页面(重定向 HTML 页面),然后显示 python 脚本页面。
I know that its not the python script thats slow because if I just go straight to the python script(no redirect from the HTML page) then it loads very fast & with no white screen.
我知道它不是很慢的 python 脚本,因为如果我直接进入 python 脚本(没有从 HTML 页面重定向),那么它加载速度非常快并且没有白屏。
So for example; someone accesses http://mywebsite.com/contactUs.htmlwhich contains only the following HTML:
例如;有人访问仅包含以下 HTML 的http://mywebsite.com/contactUs.html:
<HTML> <HEAD><META HTTP-EQUIV=Refresh CONTENT="0; url=cgi-bin/loadWebPage.py?webpage=50002"> </HEAD> </HTML>
<!-- This redirects to my python script that grabs the contact us HTML & prints it out(shows the webpage HTML) -->
Are there other ways a HTML file can redirect to a python script that will be faster?
HTML文件是否有其他方法可以重定向到更快的python脚本?
Maybe I can use HTTP messages? Would the following text/code work in a HTML file?:
也许我可以使用 HTTP 消息?以下文本/代码可以在 HTML 文件中使用吗?:
Status: 301 Moved"
Location:/cgi-bin/loadWebPage.py?webpage=50002
Would my last resort be AJAX or javascript to get the fastest result?
我最后的手段是 AJAX 还是 javascript 以获得最快的结果?
<html>
<head>
<script type="text/javascript">
<!--
window.location = 'http://mywebsite.com/cgi-bin/loadWebpage.py?webPage=50001';
-->
</script>
</head>
<body>
</body>
</html>
采纳答案by Mike Crawford
Place a file called .htaccess in the same directory as your HTML file. If the file is called "http://www.example.com/foo/bar.html" then .htaccess would contain:
将一个名为 .htaccess 的文件放在与您的 HTML 文件相同的目录中。如果文件名为“http://www.example.com/foo/bar.html”,则 .htaccess 将包含:
Redirect permanent /foo/bar.html http://www.example.com/script.py
Redirect permanent /foo/bar.html http://www.example.com/script.py
Note that the file that gets redirected does not have http://www.example.com, while the file you redirect it to needs the full URL.
请注意,被重定向的文件没有http://www.example.com,而您将其重定向到的文件需要完整的 URL。
If it's your own server, it's a little faster to disable .htaccess files, while instead putting the Redirect command in your httpd.conf file.
如果它是您自己的服务器,则禁用 .htaccess 文件会更快一些,而将 Redirect 命令放在您的 httpd.conf 文件中。
回答by Singular1ty
Don Quixote is correct about the .htaccess, I would just like to point out that
堂吉诃德关于 .htaccess 是正确的,我只想指出
http://progtuts.info/198/the-anatomy-of-a-htaccess-file-hints-and-tips/
http://progtuts.info/198/the-anatomy-of-a-htaccess-file-hints-and-tips/
Will provide some examples and a whole lot of stuff on how to edit your htaccess files etc.
将提供一些示例和大量有关如何编辑 htaccess 文件等的内容。
Redirect will definitely work.
重定向肯定会起作用。