douzhai1182 2014-12-18 18:30
浏览 36
已采纳

PHP表单和.htaccess冲突

I have a couple simple contact forms on my page. All was working well (form was sent to the email in the script) until I added this .htaccess file.

Is there a way to process the php form while also including the .htaccess file? (because it made the site look really nice)

Thanks in advance.

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

FORM CODE:

<?php
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];

$from = 'From: Contact Page';
$to = 'xxxx@xxx.com';
$subject = 'Contact Page';
$human = $_POST['human'];

$body = "From: $first_name
 $last_name
 E-Mail: $email
 Phone: $phone
 Message:
 $message";

if ($_POST['submit'] && $human == '4') {

    if (mail ($to, $subject, $body, $from)) {
        header('Location: http://www.xxxxxx.com/ThankYou.php');
        exit();
    } else {
        header('Location: http://www.xxxxxx.com/error.php');
    }
} else if ($_POST['submit'] && $human != '4') {
    header('Location: http://www.xxxxxx.com/error.php');;
}
?>
  • 写回答

1条回答 默认 最新

  • duanjuduo4573 2014-12-18 18:38
    关注

    You need to disable POST method from first redirect rule since external redirect will not carry POST data to new URL. Use this code:

    Options +FollowSymLinks -MultiViews
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /
    
    ## hide .php extension
    # To externally redirect /dir/foo.php to /dir/foo
    RewriteCond %{REQUEST_METHOD} !POST
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
    RewriteRule ^ %1 [R=302,L,NE]
    
    ## To internally redirect /dir/foo to /dir/foo.php
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^ %{REQUEST_URI}.php [L]
    

    btw this full .htaccess smells like one of my old answers on SO :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog