php - htaccess 阻止页面扩展显示

标签 php html .htaccess

我设置了 htaccess 文件,这样我的网站页面就不需要显示扩展名,我的链接设置如下,末尾有斜线:

 www.mywebsite/about/

这些链接有效 - 但如果我直接输入

 www.mywebsite/about.html 

该网站将以 html 扩展名显示此页面。有没有一种方法可以阻止显示扩展名,即使它们是直接输入的? :S

我的大部分页面都是 html,除了一个 php 页面。这是我的 htaccess 中的内容:

Options -Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)/$ $1.html [L]
RewriteRule ^php-page/([^/\.]+)/?$ php-page.php?p=$1 [L]

如果有人能帮我解决这个问题,我将不胜感激! :)

最佳答案

请试试这个:

Options -Indexes
Options +FollowSymLinks
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^(.*)\.html$
RewriteRule .* %1/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301]

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)/$ $1.html [L]

RewriteRule ^php-page/([^/\.]+)/?$ php-page.php?p=$1 [L]

我认为它可以做得比这更好一些,但它应该可以工作。

关于php - htaccess 阻止页面扩展显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8467420/

相关文章:

javascript - 使用 jquery 获取的 url 参数扩展 Accordion 菜单并将 attr 添加到所需的类

.htaccess 重写规则并在 url 中进行字符串替换

PHP:get_call_class() 与 get_class($this)

javascript - 如何发送和检索信息/

php - 使用 Visual Studio 调试 PHP 应用程序中的 JavaScript

PhpStorm 检查错误或错误代码? try block 中未抛出的异常是意外的

python - 获取具有任意嵌套度的 html 元素的内容(以及内容的 xpath)

html - 当有很多输入时,Chrome 很慢

javascript - PHP:只有在成功登录后才能访问文件夹的文件

php - 使用 htaccess 从 URL 中删除文件夹名称