John Doeherskij John Doeherskij - 4 months ago 9
PHP Question

htaccess redirect to a maintainance file

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?

Answer

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>