根据 URL 显示内容

Display content based on URL

在我的站点中,我希望制作更简单的 URL,例如: http://www.example.com/about

而不是: http://www.example.com/about.php

主要是为了美观,现在如果请求目录,我会将 Apache 重定向到 ./default.php。然而,这迫使我创建一个目录 http://www.example.com/about/ 和其中一个名为 default.php 的文件,结尾为: http://www.example.com/about/default.php

我知道有更好的方法,可能使用 PHP 或 JS,怎么办?

都在apache的配置里, rewriteEngine 可以为你做到这一点

http://httpd.apache.org/docs/current/mod/mod_rewrite.html http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

http://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/


摘自第三个 link: 的片段 要从 PHP 文件(例如 yoursite.com/wallpaper.php 到 yoursite.com/wallpaper 中删除 .php 扩展名,您必须将以下代码添加到您的阿帕奇配置:

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

如果要删除 .html 扩展名:

RewriteRule ^([^\.]+)$ .html [NC,L]

就是这样!您甚至可以在 HTML 文档中添加 link 页面,而无需添加页面扩展名。例如:

<a href="http://whatever.com/wallpaper" title="wallpaper">wallpaper</a>