【问题标题】:.htaccess File Extension Remover Code Breaks Website Form.htaccess File Extension Remover 代码破坏网站表单
【发布时间】:2014-01-09 19:15:45
【问题描述】:

所以我在我的 HTACCESS 中有这段代码,这很棒,因为如果访问者进入带有 .php 文件扩展名的页面,它首先从我的页面中删除 .php 文件扩展名,然后它允许页面在没有扩展名的情况下加载. (所以它只是美化了 URL)

# REMOVE FILE EXTENSIONS
RewriteEngine On

# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]

# check to see if the request is for a PHP file
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]

效果很好,但后来我在页面上遇到问题:http://www.CyberBytesInc.com/contact,因为我有一个调用 .php 文件发送的表单:

<form id="request-form" action="resources/script/question-send.php" method="post">

上面的 htaccess 代码删除了这个文件的 .php,我得到错误代码“不允许直接访问这个页面”。这是脚本内部的,它是

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

一旦我从 htaccess 中删除它,它就会开始工作:

# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]

但是,如果将 .php 放在页面末尾(Google 的大部分内容都使用文件扩展名编入索引,我正在尝试删除它),我并没有得到删除文件扩展名的好处。

我想如果我能以某种方式使 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.');

}

【问题讨论】:

  • 这部分的完整代码... else { die('不允许直接访问此页面。'); }
  • 刚刚加在上面,谢谢!
  • 如果你把这个action="resources/script/question-send.php" ..改成这个:action="resources/script/question-send"的形式??
  • 不,我已经尝试过了,因为我认为它必须工作。当它没有/时让我很困惑:
  • 没问题,我很乐意为您提供帮助...几年前,当我开始编写这些重写规则时,我很头疼。

标签: javascript php apache .htaccess mod-rewrite


【解决方案1】:

更改您的规则以跳过POST 请求:

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

【讨论】:

  • 谢谢你,我以后会试试这个。我设法找出问题所在。不过,这绝对是一个很好的解决方案!
  • 很高兴您的问题得到解决并且您喜欢我的回答
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-11
  • 1970-01-01
  • 2018-03-26
  • 2015-12-01
相关资源
最近更新 更多