【问题标题】:.htaccess only allow access and redirect to root index.php for only one directory.htaccess 仅允许访问并重定向到根 index.php 仅用于一个目录
【发布时间】:2014-05-26 08:38:19
【问题描述】:

我在我的新 php 项目中遇到了一些 htaccess 问题。我找不到任何解决方案,所以我在这里问,希望有人能帮助我。

1 .我需要什么:

在根目录下,无法访问指向现有文件和文件夹的 URL,除了 /Public/ 及其所有文件夹和文件(仅允许 /Public/ 访问)。指向不存在的文件和文件夹的 URL 将被重定向到 root index.php。 /Public/ 也被重定向到 root index.php ,其中的文件夹和文件不是(直接访问)。

2 。我尝试过但不起作用的方法:

<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteBase /
 RewriteRule ^index\.php$ - [L]
 RewriteCond %{REQUEST_URI} !^/Public
 RewriteCond %{REQUEST_FILENAME} !^index\.php$
 RewriteCond %{REQUEST_FILENAME} -f
 RewriteCond %{REQUEST_FILENAME} -d
 RewriteRule . - [F,L,NC]
 RewriteBase /Public/
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule . /index.php [L,QSA]
</IfModule>
------------------------------------
<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteBase /
 RewriteRule ^index\.php$ - [L]
 RewriteCond %{REQUEST_URI} !^/Public
 RewriteRule . - [F,L,NC]
 RewriteBase /Public/
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule . /index.php [L,QSA]
</IfModule>
------------------------------------
<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteBase /
 RewriteRule ^index\.php$ - [L]
 RewriteCond %{REQUEST_FILENAME} -f
 RewriteCond %{REQUEST_FILENAME} -d
 RewriteRule . - [F,L,NC]
 RewriteBase /Public/
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule . /index.php [L,QSA]
</IfModule>

提前谢谢你:)

【问题讨论】:

  • 你在linux上?检查 apache 用户是否可以访问公用文件夹以外的文件
  • 我在 windows 上做我的项目。但是完成后我会在一些linux环境下测试它

标签: php apache .htaccess mod-rewrite redirect


【解决方案1】:

在 root .htaccess 中试试这个代码:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !^public /index.php [L,NC]

【讨论】:

  • 感谢您的回复。但这会为所有 url 返回 403 Forbidden
  • 谢谢先生。但它仍然导致根文件夹为 403,不存在的文件和文件夹为 404 :(
  • 我猜你的要求不清楚。您不想阻止任何不公开的内容吗?
  • 我可以在这里解释我的要求:我的文件结构是 Root{ "index.php","Some folder contains class files" , "Plugins folder" , "Public folder" } 我需要允许仅在“公共文件夹”内访问,并将所有其他访问移至根目录内的 index.php
  • 很高兴知道,您能否通过单击我的答案左上角的勾号将答案标记为已接受。
猜你喜欢
  • 2015-08-07
  • 2013-12-31
  • 2017-01-12
  • 2012-04-27
  • 2012-12-18
  • 2014-06-21
  • 2013-03-06
  • 2014-01-04
  • 2016-11-22
相关资源
最近更新 更多