.htaccess在特定输入时删除文件类型扩展名

时间:2012-03-04 19:00:05

标签: .htaccess mod-rewrite

目前我的.htaccess文件将接受以下格式的链接: website.com/file(自动添加.php) 甚至是一个扩展名不正确的人: website.com/file.foo(删除扩展名并将其替换为.php)

但是,如果用户使用正确的扩展名输入网址,例如: website.com/file.php,扩展程序不会被隐藏。这不是很美观,所以我想删除它。但是,我尝试的所有方法都会导致无限重定向循环。

我该怎么做? 我目前的代码是:

#If the URL does not exist, try removing the URL's file type
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([A-Za-z0-9_/]+)\.([A-Za-z0-9_/]+)$ /$1 [NC,R]

#If the URL does not exist, but the URL+.php exists, then append the .php extension to it
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME}.php -f
RewriteRule ^([A-Za-z0-9_/-]+)$ /$1.php [NC,L]

#If the URL does not exist, but the URL+.html exists, then append the .html extension to it
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule ^([A-Za-z0-9_/-]+)$ /$1.html [NC,L]

#If the URL does not exist and it's got the .php extension, get rid of the .php extension
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.+)\.php$ /$1 [NC,L]

1 个答案:

答案 0 :(得分:0)

尝试更改您的第一个规则部分,如下所示

#If the URL does not exist, try removing the URL's file type
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([A-Za-z0-9_/]+)\.([A-Za-z0-9_/])(\?|\ ) [NC]  
RewriteRule ^ /%1 [NC,R=301]