htaccess重定向到维护文件

I have an htaccess in the public directory:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    RewriteBase /sub/web1/

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # HERE I NEED SOM MAGICAL ONELINER TO REDIRECT STUFF TO MAINTENANCE.HTML
    # BUT NO IDEA HOW TO WRITE IT ;(

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

and I want to add some rule inside, so when I uncomment the rule, all links that hit http://example.com/sub/web1/ or any files down that line, will be redirected to http://example.com/sub/web1/maintenance.html

I have tried to add: DirectoryIndex maintenance.html but this only redirects http://example.com/sub/web1/, if I have some subfolder or specific files like http://example.com/sub/web1/posts, it is useless.

Is there some oneliner that can even pull the domain name so it hasn't have to be typed absolutely? So, the example.com - or whatever domain - is not needed to type in the rule?

You can have a rule like this:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On
    RewriteBase /sub/web1/

    # uncomment line below to route everything to maintenance.html
    # RewriteRule !^maintenance\.html$ maintenance.html [L,NC]

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # HERE I NEED SOM MAGICAL ONELINER TO REDIRECT STUFF TO MAINTENANCE.HTML
    # BUT NO IDEA HOW TO WRITE IT ;(

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

You can use the %{HTTP_HOST} variable to get the current domain that was used when hitting the given htaccess file - here's a handy cheat sheet of things:

askapache - cheatsheet

And some examples

askapache - examples

Although it's worth trying this out as it'll use root ^$ and rewrite that to the sub directory you want to use:

RewriteBase /
ReqwriteRule ^$ /sub/web1/

Although this has already been answered and accepted, it's worth mentioning that if your maintenance page is temporary, you really should be returning the right http response, otherwise search engines might drop or re-index pages incorrectly.

At my company we use something like this:

RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/holding/holding.html -f
RewriteCond %{DOCUMENT_ROOT}/holding/holding.enable -f
RewriteCond %{SCRIPT_FILENAME} !holding.html
RewriteRule ^.*$ /holding/holding.html [R=503,L]
ErrorDocument 503 /holding/holding.html

We create holding.html as the maintenance page, and then touch holding.enable to make the server switch to it without requiring a restart (not forgetting to rm it when we're done). The 503 return code on the rewrite rule, and the ErrorDocument 503 ensure that search engines see this a temporary outage and don't start de-listing our pages.