$_POST empty after form submit with htaccess redirect

≡放荡痞女 提交于 2021-02-10 23:49:29

问题


I'm experiencing a problem with an empty $_POST array after submitting a html form.

<form action="interview/booking_summary" method="POST">
    <p>Ihr Name: <input type="text" name="name" /></p>
    <p>Ihr Alter: <input type="text" name="alter" value="23" /></p>
    <p><input type="submit" name="submit" /></p>
</form>

I think it has to do with my .htaccess file. This seems to redirect pages (e.g. file.php will be shown as file (without .php suffix)

Unfortunately I'm just working on a site I did not program by myself so I don't really understand what the htaccess file does.

It has some ReWrite Rules and I think those are responsible...

ErrorDocument 403 /fehler/403/
ErrorDocument 404 /fehler/404/

# PHP Settings
# php_flag magic_quotes_gpc off
# php_flag register_globals off

RewriteEngine On
#Options +FollowSymlinks
RewriteBase /

# Verhindert Probleme mit Trailing Slashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]

# Ordner
RewriteRule cronjobs\/ - [L]
RewriteRule ^([a-z]{2,2})/rss/$ /cronjobs/rss.php?language=$1 [L]

# Admincenter
RewriteRule ^admin/$ /admin/index.php [L]

# Wartung
RewriteRule ^construction/ /wartung.php [L]

# Sprache
RewriteRule ^de/$ /index.php?lang=de
RewriteRule ^en/$ /index.php?lang=en

# Allgemein

RewriteRule ^([a-z]{0,2}/)([a-zA-Z0-9_-]+)/$ /index.php?content=$2/index&lang=$1 [L]
RewriteRule ^([a-z]{0,2}/)([a-zA-Z0-9_-]+)/page-([0-9]+)/$ /index.php?content=$2/index&page=$3&lang=$1 [L]
RewriteRule ^([a-z]{0,2}/)([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ /index.php?content=$2/single&id=$3&lang=$1 [L]
RewriteRule ^([a-z]{0,2}/)([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/add-([a-zA-Z0-9_-]+)/$ /index.php?content=$2/single&id=$3&add=$4&lang=$1 [L]
RewriteRule ^([a-z]{0,2}/)([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/page-([0-9]+)/$ /index.php?content=$2/single&id=$3&page=$4&lang=$1 [L]
RewriteRule ^([a-z]{0,2}/)([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/edit-([a-zA-Z0-9_-]+)/$ /index.php?content=$2/single&id=$3&edit=$4&lang=$1 [L]
RewriteRule ^([a-z]{0,2}/)([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/delete-([a-zA-Z0-9_-]+)/$ /index.php?content=$2/single&id=$3&delete=$4&lang=$1 [L]
RewriteRule ^([a-z]{0,2}/)([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ /index.php?content=$2/single&id=$3&permalink=$4&lang=$1 [L]

# Error Dokumente
RewriteRule ^fehler/([0-9]+)/$ /index.php?content=error&code=$1 [L]

RewriteRule ^booking-summary booking-summary.php    [L]


#@__HCP_END__@#
# Anything after the comment above is left alone

I tried to find a solution and came across this thread: PHP method="post" stopped working after I added this .htaccess... Why?

And so I tried to add a rewrite rule saying:

RewriteRule ^booking-summary      booking-summary.php    [L]

But unfortunately this did not do the trick. Can you tell me how I can make an exeption for this php file so that I can access my $_POST array?

Thanks in advance.

Cheers

M


回答1:


See if this helps. You don't really have a rule that matches this your post URL. If this is your link interview/booking_summary, then it needs to be in the rule because I don't see one there that takes care of this already.

Add a / to your form URL so you don't have to go messing with existing rules.

<form action="interview/booking_summary/" method="POST">

Then try this rewriterule providing booking-summary is in interview folder.

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^interview/booking-summary/?$ /interview/booking-summary.php [L]



回答2:


You have to set the http code of the redirect to 307 to allow redirects with POST data:

[R=307,L]

307 tells the browser to retry the request with the new url using the same method. 302 and 303 tells the browser to use GET instead.




回答3:


Possibly this rule:

# Verhindert Probleme mit Trailing Slashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]

Is the culprit. This is redirecting URL's so they have a trailing slash, and as your form action does not have a trailing slash it's being redirected and losing the POST values in the process. Try changing the form action to this:

<form action="interview/booking_summary/" method="POST">


来源:https://stackoverflow.com/questions/28967659/post-empty-after-form-submit-with-htaccess-redirect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!