Nginx html 表单中的尾部斜杠

问题描述 投票:0回答:1

我在下面有 nginx 配置。我添加了“重写 ^([^.]*[^/])$ $1/ permanent;”行确保当我的网站页面没有尾部斜杠时被访问时,它们仍然有效。但是,当我有一个 html 表单,其操作没有尾随斜杠时,该表单不会提交,页面只是重新加载或转发。我无法弄清楚到底是哪个。

server {
    listen 80;
    listen [::]:80;
    server_name mysite.com;
    
    rewrite ^([^.]*[^/])$ $1/ permanent;
    absolute_redirect off;
    server_name_in_redirect on;

    location / {
        proxy_pass http://webserver:80/;
    }

    location ~ /.well-known/acme-challenge {
        allow all;
        root /tmp/acme_challenge;
    }
}

不带尾部斜杠的 html 表单:

<form method='post' action='action/submit'>

带有尾部斜杠的 html 表单:

<form method='post' action='action/submit/'>

htaccess:

RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(css)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php [L,QSA]

我该怎么做才能确保表单不需要尾部斜杠?

.htaccess nginx url-rewriting trailing-slash
1个回答
0
投票

但是,当我有一个带有不带尾部斜杠的操作的 html 表单时,该表单不会提交,页面只是重新加载或转发。

因为当浏览器以 301 permanent(或 302 temporary)HTTP 状态重定向时,重定向的(第二个)请求将变成 GET 请求,因此初始请求中可能存在的任何 POST 数据都会丢失.

但是,这是一个内部 HTML 表单,您应该已经将请求提交到规范 URL(带有尾部斜杠) - 它不应该 永远 被重定向。 (重定向大概只针对外部访问者?)

如果您确实需要通过重定向保留 HTTP 请求方法,请使用 308 永久(或 307 临时),这将保留 POST 数据。然而,在这种情况下这不是必需的,因为您实际上应该首先发布到规范 URL。

旁白: 为什么要包含

.htaccess
(Apache) 配置文件;这大概是一个 Nginx 服务器?!

© www.soinside.com 2019 - 2024. All rights reserved.