php - .htaccess 只允许访问 index.php 和一个目录

标签 php regex apache .htaccess mod-rewrite

我正在使用

RewriteEngine On
RewriteRule ^.*$ ./index.php

通过 index.php 重定向我所有的网站流量,让我的 php 代码获取 request_uri 并相应地路由每个请求。

我想限制对除包含 js 文件、css 文件、图像等的文件/目录之外的所有其他文件/目录的访问。

我发现我可以使用:

Order deny,allow
Deny from all

<Files index.php>
    Order Allow,Deny
    Allow from all
</Files>

然后在公共(public)目录中有另一个 .htaccess 文件以允许某些文件类型。

当我导航到 localhost/mysite/index.php 时,它工作正常,因为 index.php 文件是可访问的。

但对于任何其他网址,我都会收到 403,例如/mysite/home/被禁止,即使我希望它能正常工作。

我怎样做才能获得预期的行为?

我的完整 .htacces 文件是:

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

RewriteRule ^.*$ ./index.php


Order deny,allow
Deny from all

<Files index.php>
    Order Allow,Deny
    Allow from all
</Files>

编辑:/public/.htaccess 目前看起来像这样:

<Files ~ "\.(xml|css|jpe?g|png|gif|js|pdf)$">
  Allow from all
</Files>

最佳答案

/mysite/.htaccess 中尝试这段代码:

DirectoryIndex index.php
RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^ index.php [L]

RewriteRule !^(public/|index\.php) [NC,F]

关于php - .htaccess 只允许访问 index.php 和一个目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30446569/

相关文章:

php - Drupal 7 token 替换

python - 通过添加一些内容来修改另一个正则表达式

apache - Httpd 启动时没有错误

php - 如何使用 php 从 url 和 301 重定向获取变量?

php - 在 PHP 中打印到打印机

php - java查询链接错误

PHP 或 SQL 问题

javascript - 如何从字符串中删除所有空格和回车符?

java - 目录中文件的正则表达式

python - Django 中的 HTTP_AUTHORIZATION header