首页 > 解决方案 > 在 apache 中删除 .html 和 .php 的扩展名

问题描述

代码混合在一起,我在同一个文件夹中有 .html 和 .php 。永远不会有相同的名称但不同的扩展名。我有一些应该可以工作但没有的代码。html 部分有效,但是当我尝试请求没有扩展名的 PHP 页面时,URL 会给出 404

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

标签: phphtmlapachemod-rewriteurl-rewriting

解决方案


你可以在 .htaccess 文件上试试这个:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule (.*) $1.html [L]

推荐阅读