php - 在 php 上使用 .htaccess 文件重写 URL

标签 php html .htaccess

我正在使用 htaccess 进行动态 url 重写。我想将 test.php 显示为 test.html

htaccess 代码是

Options +FollowSymlinks

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ $1.php [nc]

运行test.php后,浏览器在地址栏显示test.php。但如果我将 test.php 更改为 test.html,该页面将提供相同的内容。我的 apache 配置文件很好。我该如何解决这个问题? 感谢帮助我..

最佳答案

您不能拒绝访问您的 .php

你需要在 html 链接中放入你的 a 标签,它将在后台转到 php。

如果您仍然只想播种 html 地址,您可以将正则表达式放入您的 php 文件中,并检查扩展名是否为 php。如果是这样,将页面重定向到 html。

例子:

<?php 
     $temp = $_SERVER['PHP_SELF'];
     $temp = explode('.', $temp);
     if($temp[count($temp)-1] == "php"){
         header("Location: test.html", true, 301);
     }
?>

* 没尝过!!! *

关于php - 在 php 上使用 .htaccess 文件重写 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13340059/

相关文章:

php - 删除图像行并从目录中删除图像

php - 提交详细信息后如何检索 id

php mysql 没有以正确的方式更新行

html - 绕过 CSS 背景不打印的最佳方法

HTML 定位 : overlap between navigation bar and text

php - htaccess : disallow all pdf files but allow from php script (file download script)

php - 具有自定义值的 Paypal 结帐表单

html - Logo 左对齐桌面/然后在移动设备上居中 - Bootstrap

php - 没有收到 POST 数据 Symfony

php - 在 Apache 服务器上将特定的 .htm 页面处理为 .php