I have the nginx config down below. I added the line "rewrite ^([^.]*[^/])$ $1/ permanent;" to make sure that when my website pages are visited when they have no trailing slash, they still work. However, when I have a html form with an action without trailing slash, the form is not submitted and the page is just reloaded or forwarded. I cannot figure out which exactly.
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 without trailing slash:
<form method='post' action='action/submit'>
html form with trailing slash:
<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]
What can I do to make sure the form doesn't need a trailing slash?
Because when the browser is redirected with a 301 permanent (or 302 temporary) HTTP status, the redirected (second) request becomes a GET request and so any POST data that might have been on the initial request is lost.
However, this is an internal HTML form, you should already be submitting the request to the canonical URL (with a trailing slash) to begin with - it should never be redirected. (The redirect is presumably only for external visitors?)
If you do need to preserve the HTTP request method through the redirect then use a 308 permanent (or 307 temporary) and this will preserve the POST data. HOWEVER, this should not be necessary in this case as you really should be POSTing to the canonical URL to begin with.
Aside: Why have you included a
.htaccess(Apache) config file; this is presumably an Nginx server?!