Javascript .htaccess文件扩展名删除程序代码破坏网站表单

Javascript .htaccess文件扩展名删除程序代码破坏网站表单,javascript,php,apache,.htaccess,mod-rewrite,Javascript,Php,Apache,.htaccess,Mod Rewrite,因此,我的HTACCESS中包含了这段代码,这段代码非常棒,因为如果访问者以.php文件扩展名进入页面,它首先会从我的页面中删除.php文件扩展名,然后允许页面加载而不加载扩展名。(因此,它只是对URL进行了预绑定) 它工作得很好,但随后我在页面上遇到了问题:因为我有一个表单调用.php文件发送: <form id="request-form" action="resources/script/question-send.php" method="post"> 但一旦我将其从hta

因此,我的HTACCESS中包含了这段代码,这段代码非常棒,因为如果访问者以.php文件扩展名进入页面,它首先会从我的页面中删除.php文件扩展名,然后允许页面加载而不加载扩展名。(因此,它只是对URL进行了预绑定)

它工作得很好,但随后我在页面上遇到了问题:因为我有一个表单调用.php文件发送:

<form id="request-form" action="resources/script/question-send.php" method="post">
但一旦我将其从htaccess中删除,它就会开始工作:

# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
但是如果把.php放在页面的末尾,我就不能得到删除文件扩展名的好处(谷歌的大部分内容都是用文件扩展名索引的,我正试图删除这个文件扩展名)

我想,如果我能以某种方式使htaccess代码能够工作,除了从我的/resources/scripts/文件夹访问文件时,我不知道修复此问题的最佳方法

您可以立即访问该网站,查看它是否因此而不起作用。目前,我可能会删除上述代码行,以便我的表单至少能正常工作。因此,如果您查看该网站且表单正常工作,则是因为我删除了上述.htaccess,直到我找到如何成功将其放入其中为止

谢谢

编辑:question-send.php的完整代码

<?php

// Get email address
$email_address = 'email@site.com';

// Ensures no one loads page and does simple spam check
if( isset($_POST['name']) && empty($_POST['spam-check']) ) {

    // Declare our $errors variable we will be using later to store any errors
    $error = '';

    // Setup our basic variables
    $input_name = strip_tags($_POST['name']); //required
    $input_email = strip_tags($_POST['email']); //required
    $input_subject = strip_tags($_POST['subject']);
    $input_message = strip_tags($_POST['message']); //required

    // We'll check and see if any of the required fields are empty
    if( strlen($input_name) < 2 ) $error['name'] = '<label for="question-name">Please enter your <b>Name</b></label>';
    if( strlen($input_message) < 5 ) $error['message'] = '<label for="question-message">Please leave a longer <b>Message</b></label>';

    // Make sure the email is valid
    if( !filter_var($input_email, FILTER_VALIDATE_EMAIL) ) $error['email'] = '<label for="question-email">Please enter a valid <b>Email Address</b></label>';

    // Set a subject & check if custom subject exist
    if( $input_subject ) $subject = "(Question) - $input_subject";
    else $subject = "(Question) - No Subject";
    // $message .= "$input_message\n";
    $message .= "\n\n---\nThis email was sent by $input_name from $input_email";

    // Now check to see if there are any errors 
    if( !$error ) {

        // No errors, send mail using conditional to ensure it was sent
        if( mail($email_address, $subject, $message, "From: $input_email") ) {
            echo '<p class="success"><b>EMAIL SENT SUCCESSFULLY.</b><br />' . "Dear $input_name, " . 'thank you for contacting CyberBytes Inc. Please allow us <b>24-48</b> hours to review your request and get back to you. If you need a response sooner, please contact us via telephone at (716) 876-1824.<br /><br /><b>Please verify that this is your correct Email Address:</b><br />' . "Email Address: <i>$input_email</i>" . '<br /><br /><span class="red"><b>PLEASE NOTE:</b></span><br /> If we do not respond to your request within a reasonable amount of time, please give us a call as there may have been an error on our end with your request.</p>';
        } else {
            echo '<p class="error">There was a problem sending your email! Please give us a call at (716) 876-1824 as there seems to be an error on our end with the form.</p>';
        }

    } else {

        // Errors were found, output all errors to the user
        $response = (isset($error['name'])) ? $error['name'] . "\n" : null;
        $response .= (isset($error['email'])) ? $error['email'] . "\n" : null;
        $response .= (isset($error['message'])) ? $error['message'] . "\n" : null;

        echo "<p class='error'>$response</p>";

    }

} else {

    die('Direct access to this page is not allowed.');

}

将规则更改为跳过
POST
请求:

# browser requests PHP
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} ^\s/([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]

此部分的完整代码…否则{die('不允许直接访问此页面');}只在上面添加了它,谢谢!如果您更改此操作=“resources/script/question send.php”。。改为:action=“resources/script/question send”在形式上??不,我已经尝试过了,因为我认为它必须工作。当它没有工作时让我非常困惑/:没问题,我很高兴能帮助…几年前我开始使用那些重写规则时,我有很多头疼的事。感谢这一点,我将来会尝试。我设法找出了问题所在。这绝对是一个很好的解决方案我也是!很高兴你的问题解决了,你喜欢我的答案
<?php

// Get email address
$email_address = 'email@site.com';

// Ensures no one loads page and does simple spam check
if( isset($_POST['name']) && empty($_POST['spam-check']) ) {

    // Declare our $errors variable we will be using later to store any errors
    $error = '';

    // Setup our basic variables
    $input_name = strip_tags($_POST['name']); //required
    $input_email = strip_tags($_POST['email']); //required
    $input_subject = strip_tags($_POST['subject']);
    $input_message = strip_tags($_POST['message']); //required

    // We'll check and see if any of the required fields are empty
    if( strlen($input_name) < 2 ) $error['name'] = '<label for="question-name">Please enter your <b>Name</b></label>';
    if( strlen($input_message) < 5 ) $error['message'] = '<label for="question-message">Please leave a longer <b>Message</b></label>';

    // Make sure the email is valid
    if( !filter_var($input_email, FILTER_VALIDATE_EMAIL) ) $error['email'] = '<label for="question-email">Please enter a valid <b>Email Address</b></label>';

    // Set a subject & check if custom subject exist
    if( $input_subject ) $subject = "(Question) - $input_subject";
    else $subject = "(Question) - No Subject";
    // $message .= "$input_message\n";
    $message .= "\n\n---\nThis email was sent by $input_name from $input_email";

    // Now check to see if there are any errors 
    if( !$error ) {

        // No errors, send mail using conditional to ensure it was sent
        if( mail($email_address, $subject, $message, "From: $input_email") ) {
            echo '<p class="success"><b>EMAIL SENT SUCCESSFULLY.</b><br />' . "Dear $input_name, " . 'thank you for contacting CyberBytes Inc. Please allow us <b>24-48</b> hours to review your request and get back to you. If you need a response sooner, please contact us via telephone at (716) 876-1824.<br /><br /><b>Please verify that this is your correct Email Address:</b><br />' . "Email Address: <i>$input_email</i>" . '<br /><br /><span class="red"><b>PLEASE NOTE:</b></span><br /> If we do not respond to your request within a reasonable amount of time, please give us a call as there may have been an error on our end with your request.</p>';
        } else {
            echo '<p class="error">There was a problem sending your email! Please give us a call at (716) 876-1824 as there seems to be an error on our end with the form.</p>';
        }

    } else {

        // Errors were found, output all errors to the user
        $response = (isset($error['name'])) ? $error['name'] . "\n" : null;
        $response .= (isset($error['email'])) ? $error['email'] . "\n" : null;
        $response .= (isset($error['message'])) ? $error['message'] . "\n" : null;

        echo "<p class='error'>$response</p>";

    }

} else {

    die('Direct access to this page is not allowed.');

}
# browser requests PHP
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} ^\s/([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]