此代码段适用于删除 php 扩展
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
但是,如果我添加 html 条件,它将不起作用
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
我做错什么了吗?
此代码段适用于删除 php 扩展
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
但是,如果我添加 html 条件,它将不起作用
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
我做错什么了吗?
你可以试试这个,在根目录的一个 .htaccess 文件中:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !\.php [NC]
RewriteRule .* %{REQUEST_URI}.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_URI} !\.html [NC]
RewriteRule .* %{REQUEST_URI}.html [NC,L]
您应该知道代码中的实现仅在请求没有尾部斜杠时才有效。我刚刚修改了您的代码以使其正常工作,但我认为这个问题毫无价值。例如:
对于类似的请求http://example.com/filename/
,与条件匹配的文件RewriteCond %{REQUEST_FILENAME}.php -f
是filename/.php
. 该文件不存在,因此,将永远不会使用相应的规则。
RewriteEngine On
在前面的代码之后添加这些行:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /(.*[^/]+)/$ [NC]
RewriteRule .* /%1 [NC,L]