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


htaccess - Rewrite any URI to find a php file

我看到的所有.htaccess文件示例都是特定于特殊页面的,但是PHP站点是否有更好的方法来重写静态PHP网站的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}/$1'.php -f
RewriteRule ^(.+)/?$ $1.php [L]