假设我有一个表格,现在正在发帖:
<form id="post-form" class="post-form"
action="/questions/ask/submit" method="post">
您会注意到 中没有站点,action
浏览器跟随它从哪里获取页面。
浏览器在发布时遵循当前的域规则
If the current page is... then the browser will POST to
======================================= =============
http://stackoverflow.com/questions/ask http://stackoverflow.com/questions/ask/submit
https://stackoverflow.com/questions/ask https://stackoverflow.com/questions/ask/submit
但我想确保浏览器始终会转到安全页面:
http://stackoverflow.com/questions/ask https://stackoverflow.com/questions/ask/submit
通常你会尝试这样的事情:
<form id="post-form" class="post-form"
action="https://stackoverflow.com/questions/ask/submit" method="post">
除非需要知道托管站点的域和虚拟路径名(例如stackoverflow.com
)。如果网站发生了变化:
stackoverflow.net
stackoverflow.com/mobile
de.stackoverflow.com
stackoverflow.co.uk/fr
beta.stackoverflow.com
那么表格action
也必须更新:
<form id="post-form" class="post-form" action="https://stackoverflow.net/questions/ask/submit" method="post">
<form id="post-form" class="post-form" action="https://stackoverflow.com/mobile/questions/ask/submit" method="post">
<form id="post-form" class="post-form" action="https://de.stackoverflow.com/questions/ask/submit" method="post">
<form id="post-form" class="post-form" action="https://stackoverflow.co.uk/fr/questions/ask/submit" method="post">
<form id="post-form" class="post-form" action="https://beta.stackoverflow.com/questions/ask/submit" method="post">
如何指示浏览器转到页面https
的版本?
假设语法:
<form id="post-form" class="post-form" action="https://./questions/ask/submit" method="post">
302、303、307
最初302 Found
是告诉用户代理去其他地方的代码。目的是浏览器会在新位置再次尝试:
POST /questions/ask/submit
302 Found
Location: /submitquestion
POST /submitquestion
不幸的是,所有浏览器都弄错了,并且总是错误地GET
在新位置上使用:
POST /questions/ask/submit
302 Found
Location: /submitquestion
GET /submitquestion
因为浏览器弄错了,所以创建了两个新代码:
302 Found
:(不推荐使用旧版)在新位置再次尝试完全相同的操作;但旧版浏览器正在切换到GET
303 See Other
:忘记用户在做什么,然后GET
在这个位置上做一个307 Temporary Redirect
:在新位置再次尝试完全相同的事情(不,说真的,同样的事情 - 我没有说你可以切换到GET
。如果你正在做,DELETE
那就去DELETE
这里。)
例如,如果用户代理在中间POST
:
| Response | What agent should do | What many agents actually do |
|------------------------|-----------------------|------------------------------|
| 302 Found | POST to new Location | GET from new Location |
| 303 See Other | GET from new Location | GET from new Location |
| 307 Temporary Redirect | POST to new Location | POST to new Location |
理想情况下,有一种方法可以用来告诉用户代理在安全 URL 上再次307 Temporary Redirect
尝试:POST
POST /questions/ask/submit
307 Temporary Redirect
Location: https://beta.stackoverflow.com/questions/ask/submit
POST /questions/ask/submit
我可以在 ASP.net 中使用足够好的服务器端:
if (!Request.IsSecureConnection)
{
string redirectUrl = Request.Url.ToString().Replace("http:", "https:");
Response.Redirect(redirectUrl);
//We don't want to send the client the deprecated 302 code, we want to use 307 telling them that we really want them to continue the POST, PUT, DELETE, etc
Response.StatusCode = (int)System.Net.HttpStatusCode.TemporaryRedirect;
Response.End();
}
但我不知道您是否可以在Location
.