我为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 = '[email protected]'; // 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('[email protected]'); // 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]