【问题标题】:How to remove .html and php file extensions if I am using many html files and one php file如果我使用许多 html 文件和一个 php 文件,如何删除 .html 和 php 文件扩展名
【发布时间】:2018-12-05 03:37:35
【问题描述】:

我对 html 文件使用了以下 .htaccess 代码:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

而且效果很好。

然后我尝试为它们(html 和 php)使用这样的代码:

RewriteEngine on

RewriteBase /
RewriteCond %{http://jobler.ro/} !(\.[^./]+)$
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule (.*) /$1.html [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+)\.html\ HTTP
RewriteRule ^([^.]+)\.html$ http://jobler.ro/$1 [R=301,L]

它没有用。因为我的 html 文件是可以单击的链接,但 php 文件不是链接。它是一个具有 phpmailer 的外部文件,当用户单击发送按钮时,它会发送一封电子邮件。 这是我的 php 文件(contact.php):

<?php

if(isset($_POST['submit'])){

          require 'PHPMailer\class.phpmailer.php';
          require 'PHPMailer\class.smtp.php';
          require 'PHPMailer\PHPMailerAutoload.php';
$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    //Server settings
    $mail->SMTPDebug = 0;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'mygmail@gmail.com';                 // SMTP username
    $mail->Password = 'mypassword9876543210';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom($_POST['email']);
    $mail->addAddress('mygmail@gmail.com');     // Add a recipient
    $mail->addReplyTo($_POST['email']);

    //Attachments
    // $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    // $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = $_POST['subject'];
    $mail->Body    = $_POST['msg'];
    $mail->AltBody = $_POST['subject'];

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
}

当用户点击此按钮时:

<div class="text-center">
  <button type="submit" class="btn btn-start-order" name="submit">Send 
    Message</button>
</div>

php 文件发送电子邮件,但它也在 url 中显示contact.php。 我怎样才能摆脱 website.com/contact.php 显示并在发送消息后只显示 website.com/contact。对不起,很长的问题。谢谢。

【问题讨论】:

    标签: php html .htaccess phpmailer contact-form


    【解决方案1】:

    尝试在您的 .htaccess 中删除 .php 扩展名

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^\.]+)$ $1.php [NC,L]
    

    编辑: 试试这个, 它可能是您正在寻找的东西,也可能不是,但我希望它会有所帮助。

    RewriteEngine On
    
    RewriteBase /
    
    RewriteCond %{THE_REQUEST} (.).php
    RewriteRule ^(.*).php $1.html [R=301,L]
    RewriteRule ^(.*).html $1.php [L] 
    

    【讨论】:

    • 感谢您的宝贵时间。它不适用于 php 文件,因为它不是我的 index.html 文件中的链接,它只接受提交按钮。再次感谢
    • @AhmedAmiin 没问题,我认为它可能会有所帮助,我已经改变了我的答案并希望它有效,除非你找到了解决方案,如果你找到了,你可以与社区分享,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-13
    • 2012-05-14
    • 1970-01-01
    • 1970-01-01
    • 2016-12-28
    相关资源
    最近更新 更多