Php 除了1个目录和1个文件外,如何使用htaccess重定向所有目录和文件?

Php 除了1个目录和1个文件外,如何使用htaccess重定向所有目录和文件?,php,.htaccess,redirect,mod-rewrite,Php,.htaccess,Redirect,Mod Rewrite,这是我的设置。 我有example.com,它有文件和子目录。其中一个文件是maintenance.php,其中一个目录是/admin。我正在尝试将所有请求从example.com重定向到example.com/maintenance.php,example.com/maintenance.php除外。不这样做会导致错误,并且example/admin中的所有页面都会出错。example.com/file1应重定向到example.com/maintenance.php,example.com/

这是我的设置。 我有example.com,它有文件和子目录。其中一个文件是maintenance.php,其中一个目录是/admin。我正在尝试将所有请求从example.com重定向到example.com/maintenance.php,example.com/maintenance.php除外。不这样做会导致错误,并且example/admin中的所有页面都会出错。example.com/file1应重定向到example.com/maintenance.php,example.com/maintenance.php不应导致任何错误,example.com/admin/file1不应重定向

这就是我无法工作的东西:

RewriteEngine On
RewriteCond %{REQUEST_URI} !(.*)admin.*
RewriteCond %{REQUEST_URI} !^/maintenance.php$
RewriteRule .* /maintenance.php [R=302,L]
example.com/admin仍然重定向到example.com/maintenance.php


谢谢大家

确保example.com/admin URL不是404未找到。当服务器尝试重定向到错误文档时,它将重定向到maintenance.php,因为错误文档不会从重定向中排除。为了避免这种情况,您可以添加一个RewriteCond规则来排除错误文档。例如:

RewriteEngine On

#Exclude *admin* and /maintenance.php
RewriteCond %{REQUEST_URI} !.*admin.*
RewriteCond %{REQUEST_URI} !^/maintenance\.php$

#This handles both 404 and 500 Errors
RewriteCond %{REQUEST_URI} !^/path/to/(404|500)\.html$

RewriteRule .* /maintenance.php [R,L]

我还避开了路径上的点,因为这样做是一种很好的做法。避免维护xphp也不重定向。

当您的站点不可用时,切勿使用外部重定向;你需要设置一个503标题,否则你可能会破坏你的搜索引擎排名

保留任何现有的重写规则,并将其添加到所有规则之前:

RewriteRule ^(?!maintenance\.php$|admin(?:$|/)) maintenance.php [NS,L]
maintenance.php的第一行应该是

<?php
header($_SERVER['SERVER_PROTOCOL'] . ' 503 Service Unavailable');
它仍然可以有内容

如果您仍然无法进入管理,您至少可以看到其他URL需要白名单。您可能还需要排除媒体、css和/或js目录


完成后,请用注释将规则注释掉,以备下次使用。

即使答案对您不起作用,您也应作出回应。