.htaccess文件扩展名卸妆code打破网站形式 [英] .htaccess File Extension Remover Code Breaks Website Form

查看:141
本文介绍了.htaccess文件扩展名卸妆code打破网站形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个code在我的.htaccess这是伟大的,因为它首先删除PHP文件扩展名从我的网页,如果访问者进入以.php文件扩展名的网页,然后它使页面加载不带扩展名。 (所以它只是prettying了URL)

 #删除文件扩展名
RewriteEngine叙述上

#浏览器请求PHP
的RewriteCond%{THE_REQUEST} ^ [AZ] {3,9} \ /([^ \] +)\。PHP
重写规则^ /?(。*)\。PHP / $ 1 [L,R = 301]

#检查是否请求的是一个PHP文件
的RewriteCond%{REQUEST_FILENAME} \ PHP -f
重写规则^ /?(。*)$ /$1.php [L]
 

它的伟大工程,但后来我碰到了页面上的问题: http://www.Cyber​​BytesInc.com/联系因为我有一个呼叫到PHP文件发送格式:

 <形式ID =请求表行动=资源/脚本/问题 -  send.php的方法=邮报>
 

和上述htaccess的code删除的.php这个文件,我得到的错误code直接访问此页面是不允许的。这是脚本的内部,它的

 }其他{
    死亡(直接访问此页面是不允许的。);
}
 

在我的htaccess的删除这虽然然后开始工作:

 #浏览器请求PHP
的RewriteCond%{THE_REQUEST} ^ [AZ] {3,9} \ /([^ \] +)\。PHP
重写规则^ /?(。*)\。PHP / $ 1 [L,R = 301]
 

但我不明白它删除文件扩展名振作,如果.PHP放置在页面(其中大部分谷歌的被索引的文件扩展名的结束,我想删除此。

我想,如果我能以某种方式使它所以只是从我的/资源/脚本/文件夹访问文件时的htaccess的code的工作,我不知道解决这个问题的最好办法。

您可以去现场,现在一看就知道它不工作,因为这个原因。暂时我可能将删除上述行code,所以我的形式ATLEAST工作。因此,如果您浏览的网站和形式的工作,那是因为我删除了上述的.htaccess直到我弄清楚如何成功地让它在那里。

谢谢!

编辑:全code的问题 - send.php

 < PHP

//获取电子邮件地址
$ EMAIL_ADDRESS ='email@site.com';

//确保没有一个负载页面,并进行简单的垃圾邮件检查
如果(使用isset($ _ POST ['名称'])及和放大器;空($ _ POST ['垃圾邮件检查'])){

    //声明我们$错误变量,我们将使用后存储任何错误
    $错误='';

    //设置我们的基本变量
    $ input_name =用strip_tags($ _ POST ['名称']); //需要
    $ input_email =用strip_tags($ _ POST ['邮件']); //需要
    $ input_subject =用strip_tags($ _ POST ['主题']);
    $ input_message =用strip_tags($ _ POST ['消息']); //需要

    //我们将检查,看看是否有任何必填字段为空
    如果(strlen的($ input_name)2)$错误['名称'] ='<标签=问题,名为>请输入您的< B>名称< / B>< /标签> ;
    如果(strlen的($ input_message)小于5)$错误['消息'] ='<标签=问题消息>请留下较长< B>消息< / B>< /标签> ';

    //确保电子邮件是有效的
    如果$错误[电子邮件] ='&LT(filter_var($ input_email,FILTER_VALIDATE_EMAIL)!);标签=的问题,通过电子邮件将>请输入一个有效的< B>电子邮件地址< / B>< /标签&gt ;';

    //设置一个受试者放大器;检查自定义主题存在
    如果($ input_subject)$主题=(问题) -  $ input_subject;
    否则$主题=(问题) - 无主题;
    // $消息=$ input_message \ N。
    。$消息=\ñ\ñ--- \ n此邮件被送往由$ input_email $ input_name;

    //现在检查,看看是否有任何错误
    如果(!$错误){

        //没有错误,使用条件,以确保它被送往发送邮件
        如果(电子邮件($ EMAIL_ADDRESS,$主题,$消息,从:$ input_email)){
            回声&其中,P类=成功>< B> EMAIL发送成功< / B>< BR /> 。 亲爱的$ input_name。 谢谢你联系Cyber​​Bytes公司请允许我们< B> 24-48< / B>时间审查你的请求,并尽快给您回复。如果你需要一个响应越快,请通过电话联系我们:(716)87​​6-1824< BR />< BR />< B>请验证这是否是您正确的邮箱地址:< / B&GT ;< BR /> 。 电子邮件地址:其中,I> $ input_email< / I>中。 '< BR />< BR /><跨度类=红>< B>请注意:< / B>< / SPAN>< BR />如果我们不给你的要求在规定时间内合理的回应,请给我们一个电话,因为可能已经在我们这边您的要求的错误和LT; / P>';
        } 其他 {
            回声&其中,P类=错误>有一个问题发送您的邮件!请给我们一个电话(716)87​​6-1824,因为似乎对我们的一端与形式的错误< / P>';
        }

    } 其他 {

        //发现错误,输出所有错误向用户
        $响应=(使用isset($错误['名称']))? $错误['名称']。 \ N:空;
        $回应。=(使用isset($错误[电子邮件]))? $错误[电子邮件。 \ N:空;
        $回应。=(使用isset($错误['消息']))? $错误['消息'。 \ N:空;

        回声&其中,P级='错误'> $应答LT; / P>中;

    }

} 其他 {

    死亡(直接访问此页面是不允许的。);

}
 

解决方案

更​​改您的规则,跳过 POST 要求:

 #浏览器请求PHP
的RewriteCond%{REQUEST_METHOD}!POST
的RewriteCond%{} THE_REQUEST ^ \ S /([^ \] +)\。PHP
重写规则^ /?(。*)\。PHP / $ 1 [L,R = 301]
 

So I had this code in my HTACCESS which was great because it first removes the .php file extension from my pages if the visitor enters the page with the .php file extension, and then it allows the page to load without the extension. (So it's just prettying up the 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]

It works great, but then I run into problems on the page: http://www.CyberBytesInc.com/contact because I have a form which calls out to a .php file to send:

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

And the above htaccess code removes the .php for this file and I get the error code "Direct access to this page is not allowed." which is inside of the script, it's the

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

Once I remove this from htaccess though then it starts working:

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

But then I don't get the perk of it removing the file extension if .php is placed at the end of the page (which much of Google is indexed with the file extension and I'm trying to remove this.

I guess if I could somehow make it so the htaccess code work except for when accessing a file from my /resources/scripts/ folder, I don't know the best way to fix this.

You can go to the site right now to see that it's not working because of this. For the time being I am probably going to remove the above mentioned line of code so my form is atleast working. So if you view the site and the form is working, it's because I removed the above .htaccess until I figure out how to successfully have it in there.

Thanks!

EDIT: Full code for 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.');

}

解决方案

Change your rule to skip POST request:

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

这篇关于.htaccess文件扩展名卸妆code打破网站形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆