11

現在投稿を行っているフォームがあるとします。

<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

ブラウザが間違っていたため、2 つの新しいコードが作成されました。

  • 302 Found: (レガシー非推奨)新しい場所でまったく同じことをもう一度試してください。しかし、レガシーブラウザはに切り替えていましたGET
  • 303 See Other: ユーザーが何をしていたかを忘れて、GETこの場所で実行します
  • 307 Temporary Redirect:新しい場所でまったく同じことをもう一度試してください(いや、まじめな話、同じことです - に切り替えることができるとは言いませんでしたGETDELETEDELETE

たとえば、ユーザー エージェントが の途中にあった場合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();
}

しかし、 で完全な uri を指定できるかどうかはわかりませんLocation

4

2 に答える 2

5

JavaScript を使用してフォームのアクションを変更できます。

var form = document.getElementById("post-form");
form.action = location.href.replace(/^http:/, 'https:');

ただし、セキュリティ上の考慮事項がいくつかあります。フォームの URL を https にリダイレクトすることをお勧めします。また、javascript から実行することもできますが、セキュリティに関しては決して javascript を信頼してはならないため、サーバーから実行してください (高速であり、ページをロードする必要はなく、http ヘッダーのみが必要です)。

于 2012-05-01T19:55:25.473 に答える