删除php扩展但拒绝直接访问

删除php扩展但拒绝直接访问,php,apache,.htaccess,Php,Apache,.htaccess,我想用htaccess删除php扩展名,但也拒绝对php文件的所有直接访问。因此,如果您编写www.site.com/test,您的ok和www.site.com/test.php将无法访问! Thx我知道这是一个老问题,但我想做同样的事情,并最终使用zerkms的答案,但进行了修改,使其在查找不存在的文件时不会中断索引和循环。下面的代码可以工作,使用“error.php”作为我的错误页面,而不是403.html RewriteEngine on #specify the error page

我想用htaccess删除php扩展名,但也拒绝对php文件的所有直接访问。因此,如果您编写www.site.com/test,您的ok和www.site.com/test.php将无法访问!
Thx

我知道这是一个老问题,但我想做同样的事情,并最终使用zerkms的答案,但进行了修改,使其在查找不存在的文件时不会中断索引和循环。下面的代码可以工作,使用“error.php”作为我的错误页面,而不是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]

我知道这是一个老问题,但我想做同样的事情,并最终使用zerkms的答案,但进行了修改,使其在查找不存在的文件时不会中断索引,也不会中断循环。下面的代码可以工作,使用“error.php”作为我的错误页面,而不是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]