Search code examples
apache.htaccesshttp-redirect

Redirecting HTTP and HTTPS requests to a temporary page - https:// is not redirecting


I wish to redirect all HTTP and HTTPS requests to https://example.com/coming-soon.html.

Here is the last .htaccess file that I tried:

# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php73” package as the default “PHP” programming language.
<IfModule mime_module>
  AddHandler application/x-httpd-ea-php73 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit


Options +FollowSymlinks
RewriteEngine on

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/coming-soon.html [L,R=302]

RewriteOptions inherit
Header set content-Security-Policy: upgrade-insecure-requests

I'm interested in always redirecting HTTP to HTTPS, that seem to work already, but in the meantime I need a redirection of entire domain to /coming-soon.html since the site is not ready yet. With the .htaccess above, HTTPS requests are not redirect to coming-soon.html.

Before, I had this other .htaccess file in place:

# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php73” package as the default “PHP” programming language.
<IfModule mime_module>
  AddHandler application/x-httpd-ea-php73 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit


Options +FollowSymlinks
RewriteEngine on

RewriteCond %{HTTPS} !=on
RewriteCond %{REMOTE_ADDR} !^123.45.67.89$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !coming-soon.html

RewriteRule ^(.*)$ /coming-soon.html [R=302,L]

RewriteOptions inherit
Header set content-Security-Policy: upgrade-insecure-requests

With this .htaccess, redirection is taking place and HTTPS works, but if i type https://example.com, it takes me to the root of the site file names in php.

I've read a lot of links but am literally either not understanding correctly or I'm doing something wrong. I'm aware of the RewriteCond for the IP it's not yet set for my IP as exception to the redirection. I can do without it the IP exception for time being.


Solution

  • To redirect http(s)://example.com/anything to https://example.com/coming-soon.html, except for a given IP address, try these .htaccess directives:

    # Tested on Debian 10 running Apache 2.4.38
    RewriteCond %{REMOTE_ADDR} !=192.168.001.001
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{REQUEST_FILENAME} !/coming-soon\.html$
    RewriteRule ^ https://%{HTTP_HOST}/coming-soon.html [R,L]
    

    In fact, only https://example.com/coming-soon.html will not be redirected; we do not want to start a loop.

    Among the .htaccess directives that you posted, these lines:

    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}/coming-soon.html [L,R=302]

    do not produce the expected redirection of https://example.com/anything to https://example.com/coming-soon.html because RewriteCond %{HTTPS} off alone does not match HTTPS requests. The addition of [OR] and an alternate RewriteCond make RewriteCond %{HTTPS} off optional.


    In the case where /coming-soon.html includes images that are grouped within an /images directory, the .htaccess directives can be:

    # Tested on Debian 10 running Apache 2.4.38
    
    # Redirect all HTTP requests to HTTPS
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}/$1 [R,L]
    
    # Except for a given IP address,
    # and for '/coming-soon.html' itself
    # and for files within '/images/',
    # redirect all requests to '/coming-soon.html'.
    RewriteCond %{REMOTE_ADDR} !=192.168.001.001
    RewriteCond %{REQUEST_FILENAME} !/coming-soon\.html$
    RewriteCond %{REQUEST_URI} !^/images/
    RewriteRule ^ https://%{HTTP_HOST}/coming-soon.html [R,L]