Apache2 重写,删除文件扩展名


Apache2 Rewrite, remove file extensions

问题

使用 apache 重写,当用户输入:

example.com/home/
OR
example.com/home

该网站会将用户发送到:

example.com/home.php

但网址将继续是:

example.com/home/

我破碎的解决方案

我目前在 .htaccess 中的代码是:

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

但它似乎将用户重定向到"主页.php.php.php.php"等等。

首先,让example.com/home/example.com/home导致相同的内容是一个主意(像谷歌这样的搜索引擎不喜欢重复的内容(。

然后,您必须禁用MultiViews才能获得所需的行为。

您可以尝试使用此代码

Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^home/$ /home.php [NC,L] 
# or ^home$ if you don't want it with trailing slash

试试这个

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

您可以访问任何没有 php 扩展名的 PHP 文件:)