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 :)

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

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改