一尘不染

如何使用.htaccess隐藏.php URL扩展?

php

我想在.htaccess文件中放一些东西,以隐藏.phpphp文件的文件扩展名,因此转到www.example.com/dir/somepage会显示它们www.example.com/dir/somepage.php。

有什么可行的解决方案吗?我的网站使用HTTPS(如果很重要)。

这是我目前的.htaccess:

RewriteEngine On

RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

ErrorDocument 404 /error/404.php

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

阅读 236

收藏
2020-05-29

共1个答案

一尘不染

在您的.htaccess中在DOCUMENT_ROOT下使用以下代码:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^ %{REQUEST_URI}.php [L]
2020-05-29