htaccess - 重写任何 URI 以查找 php 文件

htaccess - Rewrite any URI to find a php file

我看到的所有 .htaccess 文件示例都特定于一个特殊页面,但是 PHP 站点是否有更好的方法来重写静态 [=23] 的 URL =] 网站。

假设目录结构如下:

|--- index.php
|--- contact.php
|---/products/
|       |--- index.php
|       |--- features.php
|       |--- price.php

有没有办法编写 .htaccess 如果用户转到 example.com/contact/example.com/products/example.com/features/ 服务器将找到写入页面而无需在添加时指定每个页面?这是否必须在 PHP 中处理?

您可以在根 .htaccess 中使用它:

Options -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/\.php -f
RewriteRule ^(.+)/?$ .php [L]