如何解决表单发布重定向问题?

时间:2019-12-05 09:24:04

标签: php .htaccess

所以我有一个非常简单的按钮,其形式是在用户单击按钮后显示一些内容

<form class="user" action='/post.php?slug=test' method="POST">
    <button class="btn btn-info btn-md ml-0" type='submit' name="GetYourContentButton"><strong>Unlock Content</strong></button></center>
    </form>

if(isset($_POST["GetYourContentButton"])) {
  echo "This is your content";
}

问题在于帖子的官方URL是http://localhost/post/test而不是/post.php?slug=test,因此当用户单击按钮时,他将重定向到http://localhost/post.php?slug=test而不是http://localhost/post/test

令我惊讶的是,我总是收到“找不到对象!”如果我尝试更改action='/post.php?slug=test'中的表单,则会出现错误,例如action='/post/test'不起作用。

我的htaccess是

RewriteEngine On
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?post/([^/d]+)/?$ post.php?slug=$1 [L,QSA]

您知道如何解决此问题吗?

1 个答案:

答案 0 :(得分:-1)

我仍然不知道您想做什么,因为您没有按照您的问题提供从form操作执行的代码,但是,请尝试作为示例;

<?php
echo "
<form class='user' method='post'>
<input type='submit' name='GetYourContentButton' class='btn btn-info btn-md ml-0' value='Unlock Content'>
</form>
";

if(isset($_POST["GetYourContentButton"])) {
// perform your php operations here

// do a redirect afterwards
echo "<script>window.location.replace('https://example.com/somepage');</script>";

// or a timed redirect, this is set to 2000 milliseconds = 2 seconds
echo "<script>setTimeout(function() {window.open('https://example.com/somepage','_self')}, 2000);</script>";
}
?>

HT访问规则

# Disallow non https connections
rewritecond %{HTTPS} !on
rewriterule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

# Remove www prefix
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [L,NE,R=301]

# Remove html extension
rewritecond %{SCRIPT_FILENAME}.html -f
rewriterule [^/]$ %{REQUEST_URI}.html [QSA,L]
rewriterule ^(.+)\.html$ /$1 [R=301,L]

# Remove php extension
rewritecond %{SCRIPT_FILENAME}.php -f
rewriterule [^/]$ %{REQUEST_URI}.php [QSA,L]
rewriterule ^(.*)/$ $1 [R=301,L]