.htaccess:将 / 重定向到 index.php 并阻止其他所有内容
.htaccess: Redirect / to index.php and block everything else
我正在尝试保护我的 PHP 文件不被直接访问。我想要允许的是直接访问 index.php
和一个名为 public
的目录(带有 CSS、图像等)。访问根目录 /
应该重定向到 index.php
:
/ (root): allow -> redirect to index.php
+--index.php: allow
+--public
| +--... allow
+--[everything else]: block
我当前的 .htaccess
文件如下所示:
order allow,deny
<Files index.php>
Allow from all
</Files>
<Files .htaccess>
Order Allow,Deny
Deny from all
</Files>
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/$ /index.php [L]
</IfModule>
它基本上可以工作,但不会从 /
重定向到 index.php
,而是 Apache 给我一个 403 错误。我做错了什么?
查看 Order ...
的文档,您可以找到 here。
Allow,Deny
First, all Allow directives are evaluated; at least one must match, or
the request is rejected. Next, all Deny directives are evaluated. If
any matches, the request is rejected. Last, any requests which do not
match an Allow or a Deny directive are denied by default.
/
的请求不匹配任何规则,因此没有允许或拒绝指令,因此默认拒绝。您可以通过显式允许对 /
的请求并在 public 子目录中创建一个新的 .htaccess 文件以允许那里的请求来修复它。
在/.htaccess
中:
order allow,deny
<Files ~ "^(index\.php|)$">
Allow from all
</Files>
<Files .htaccess>
Order Allow,Deny
Deny from all
</Files>
DirectoryIndex index.php
在/public/.htaccess
中:
Order allow,deny
Allow from all
此工作的截屏:https://www.screenr.com/BLfN
我正在尝试保护我的 PHP 文件不被直接访问。我想要允许的是直接访问 index.php
和一个名为 public
的目录(带有 CSS、图像等)。访问根目录 /
应该重定向到 index.php
:
/ (root): allow -> redirect to index.php
+--index.php: allow
+--public
| +--... allow
+--[everything else]: block
我当前的 .htaccess
文件如下所示:
order allow,deny
<Files index.php>
Allow from all
</Files>
<Files .htaccess>
Order Allow,Deny
Deny from all
</Files>
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/$ /index.php [L]
</IfModule>
它基本上可以工作,但不会从 /
重定向到 index.php
,而是 Apache 给我一个 403 错误。我做错了什么?
查看 Order ...
的文档,您可以找到 here。
Allow,Deny
First, all Allow directives are evaluated; at least one must match, or the request is rejected. Next, all Deny directives are evaluated. If any matches, the request is rejected. Last, any requests which do not match an Allow or a Deny directive are denied by default.
/
的请求不匹配任何规则,因此没有允许或拒绝指令,因此默认拒绝。您可以通过显式允许对 /
的请求并在 public 子目录中创建一个新的 .htaccess 文件以允许那里的请求来修复它。
在/.htaccess
中:
order allow,deny
<Files ~ "^(index\.php|)$">
Allow from all
</Files>
<Files .htaccess>
Order Allow,Deny
Deny from all
</Files>
DirectoryIndex index.php
在/public/.htaccess
中:
Order allow,deny
Allow from all
此工作的截屏:https://www.screenr.com/BLfN