在使用mod_rewrite规则之后,HTML表单在提交后没有设置`$ _POST`

时间:2017-06-13 08:16:37

标签: php html apache .htaccess mod-rewrite

我有2个简单文件register.phpuser-insert.php

register.php文件保留HTML表单,方法为POST,提交按钮的名称为submit,如下所示:

<form action="user-insert.php" class="contact-form" method="post">
    <ul>                         
        <li>
            <label for="who-are-you" class="first-col">Who are you?</label>
            <select name="who-are-you" id="who-are-you" class="select">
                <optgroup label="Who are you?">
                    <option value="1">Employer</option>
                    <option value="0">Employee</option>
                </optgroup>
            </select>
        </li>
        <li>
            <label for="email" class="first-col">Email</label>
            <input id="email" type="email" name="email" placeholder="name@example.com" required />
        </li>               
    </ul>
    <input type="submit" name="submit" value="Sign Up" class="btn btn-primary" />
</form>

还有user-insert.php个文件:

session_start(); // tried to remove this, but It do nothing with this problem

// there checking for an errors
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require_once("connect.php");

if(isset($_POST['submit'])) {
    // do something
} else {
    echo "POST false";
}

总是给我POST false。没有任何错误。仅POST未设置。

问题

问题在于.htaccess,如果我从此文件中删除内容,它就会开始工作。 .htaccess现在从www.mysite.com/any-file.php重定向到www.mysite.com/any-file/。也许你有什么想法吗?

.htaccess文件如下:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} \s/+(?:(?:index|(.+))\.php)?\?lang=lt\s [NC]
RewriteRule ^ %1? [R=302,L,NE]

RewriteCond %{THE_REQUEST} \s/+([^.?]+)/?\?lang=lt\s [NC]
RewriteRule ^ %1? [R=302,L,NE]

RewriteCond %{THE_REQUEST} \s/+(?:(?:index|(.+))\.php)?\?lang=(en)\s [NC]
RewriteRule ^ %2/%1? [R=302,L,NE]

RewriteCond %{THE_REQUEST} \s/+([^.?]+)/?\?lang=(en)\s [NC]
RewriteRule ^ %2/%1? [R=302,L,NE]

RewriteCond %{THE_REQUEST} \s/+(.+)\.php\?lang=(en)&id=([^\s&]+)\s [NC]
RewriteRule ^ %2/%1/%3? [R=302,L,NE]

RewriteCond %{THE_REQUEST} \s/+(.+)\.php\?lang=(en)&id=([^\s&]+)&param=([^\s&]+)\s [NC]
RewriteRule ^ %2/%1/%3/%4? [R=302,L,NE]

RewriteCond %{THE_REQUEST} \s/+(.+\.php)\?lang=lt&(\S*)\s [NC]
RewriteRule ^ %1?%2 [L,NE,R=302]

RewriteCond %{THE_REQUEST} \s/+(.+)\.php\?id=([^\s&]+)\s [NC]
RewriteRule ^ %1/%2? [R=302,L,NE]

RewriteCond %{THE_REQUEST} \s/+(.+)\.php\?id=([^\s&]+)&param=([^\s&]+)\s [NC]
RewriteRule ^ %1/%2/%3? [R=302,L,NE]

RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.php\s [NC]
RewriteRule ^ %1%2/ [R=302,L,NE]

# url is ONLY '/en' or '/lt' -> redirect to /en/ or /lt/ (adding slash)
RewriteRule ^(en|lt)$ $1/ [R=301,L,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} !(^|&)lang=(en|lt)(&|$) [NC]
RewriteRule ^(?!(?:en|lt)/)(.+)$ lt/$1 [DPI,NC]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{DOCUMENT_ROOT}/$2.php !-f
RewriteRule ^(en|lt)/([\w-]+)/?$ - [L]

# now all urls have en/ lt/ -> parse them
RewriteRule ^(en|lt)/$ index.php?lang=$1 [L,NC,QSA]

RewriteRule ^(en|lt)/([\w-]+)/?$ /$2.php?lang=$1 [L,NC,QSA]

RewriteRule ^(en|lt)/([\w-]+)/([\w-]+)/?$ /$2.php?lang=$1&id=$3 [L,NC,QSA]

RewriteRule ^(en|lt)/([\w-]+)/([\w-]+)/([\w-]+)/?$ /$2.php?lang=$1&id=$3&param=$4 [L,NC,QSA]

1 个答案:

答案 0 :(得分:2)

使用 302重定向,重定向后不发送帖子数据。您可以使用 307 作为重定向代码来重定向发布数据。

{id: 1}