重写对index.php的所有请求(包括现有的文件和目录路径)?

时间:2018-01-25 14:49:15

标签: php apache .htaccess mod-rewrite

我正在开发一个PHP应用程序,我希望出于各种原因来管理文件服务。我需要将所有请求传递给我的index.php文件。目前我有这个适用于除了存在的文件和目录之外的所有请求。出于安全原因和内容保护,我希望在使用readfile向浏览器提供文件之前执行一些挑战,并且我希望完全阻止目录访问,这只会显示404页面的文件和目录我不希望用户看到(即私人内容,PHP脚本等)。

这是我目前的HTACCESS文件:

# Turn On Rewrite Engine
RewriteEngine On

# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_URI} !^.*$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

我不知道如何处理导致文件映射到RewriteCond文件的index.php,如果它们不存在的话。无论他们是否存在,我都希望他们重新映射。

例如,导航到" http://www.example.com/some/real/script.php"无论是否存在,都会将路径重定向到PHP。提前谢谢你们。

1 个答案:

答案 0 :(得分:1)

这个对我有用

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond %{REQUEST_URI} !/folder
RewriteRule ^(.*)$ index.php?request=$1 [L,QSA]
</IfModule>