繁体   English   中英

如果我使用许多html文件和一个php文件,如何删除.html和php文件扩展名

[英]How to remove .html and php file extensions if I am using many html files and one php file

我将以下.htaccess代码用于html文件:

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。 很抱歉这个长期的问题。 谢谢。

尝试在您的.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] 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM