当从菜单栏单击页面时,如何使.php和.html扩展名不可见


How to make the .php and .html extension invisible of a page when clicking the page from the menu bar?

我有一个带有菜单的网站。当点击菜单中的任何页面时,它应该转到特定的页面,无论是page.php还是page.html,但它不应该在url中显示.php.html。我怎样才能做到这一点呢?

我在htaccess中写了一段代码。

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}'.php -f 
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}'.html -f 
RewriteRule ^(.*)$ $1.html

这不是一个真正的php问题,而是一个apache问题。您的想法是正确的,因为您需要为此使用.htaccess文件。

但是,不要忘记确保RewriteEngine被设置为ON,并将参数放在[]括号内。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^'.]+)$ $1.php [NC,L]

你可以在这里阅读更多信息