开发者

Remove php extension but deny direct access

开发者 https://www.devze.com 2023-01-24 10:22 出处:网络
i\'开发者_StackOverflowd like to remove php extension with htaccess, but also deny from all the direct access to php file. So if you write www.site.com/test , your ok and www.site.com/test.php you can

i'开发者_StackOverflowd like to remove php extension with htaccess, but also deny from all the direct access to php file. So if you write www.site.com/test , your ok and www.site.com/test.php you cant access! Thx


I know this is an old question, but I wanted to do the same thing and wound up using the answer by zerkms, but modified so it doesn't break indexes and doesn't break with a loop when looking for files that don't exist. Here's code that works, using "error.php" as my error page instead of 403.html.

RewriteEngine on

#specify the error page
ErrorDocument 404 /error.php?e=404
ErrorDocument 403 /error.php?e=403
ErrorDocument 401 /error.php?e=401

#prevent redirect loops
RewriteCond %{ENV:REDIRECT_STATUS} !^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /error.php [L]

#redirect to .php files but prevent direct access
RewriteRule ^$ index.php [L,QSA]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)/$ $1/index.php [L,QSA]
RewriteCond %{THE_REQUEST} \.php
RewriteCond %{REQUEST_FILENAME} !error.php$
RewriteRule .* /error.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !\.php$
RewriteRule (.*) $1.php [L,QSA]
0

精彩评论

暂无评论...
验证码 换一张
取 消