Redirect entire virtualhost to 404 page

apache-2.4redirectvirtualhost

I have a few sites hosted on an apache server. Most of them can be accessed via http and https. One of them may only be accessed over http. Attempts to acccess it via https must be redirected to the 404.php page (except for attempts to access the error page itself – those should be allowed).

I can get a custom text message to appear with the ErrorDocument directive, but I'm having trouble figuring out how to send the users to the 404 page. Here's what the config looks like:

<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerName myserver
    ServerAlias myserver
    SSLEngine On
    SSLProxyEngine On

    SSLCertificateFile /path/to/certificatefile.pem
    SSLCertificateKeyFile /path/to/keyfile.pem
    Include /etc/ssl-apache.conf
    DocumentRoot /usr/local/website

    Redirect 404 /

    # This works!
    ErrorDocument 404 "Not here!"

    # This tries to redirect to the path to the 404.php file but does *not* work
    #ErrorDocument 404  /wordpress/wp-content/themes/Theme1/404.php

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

<Directory "/usr/local/website/html/wordpress">
  AllowOverride All
  Require all granted
</Directory>

<Directory />
  # required for apache 2.4+
  AllowOverride All
  Require all granted
  Options +Includes
  XBitHack on
  # allows CORS for static content
  Header set Access-Control-Allow-Origin *
</Directory>

</VirtualHost>
</IfModule>

Trying to redirect to the 404.php using ErrorDocument results in:

The requested URL / was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

I also tried ErrorDocument 404 https://myserver/wordpress/wp-content/themes/Theme1/404.php But that resulted in "too many redirects".

How can I get all requests to this VirtualHost to be redirected to the custom 404 page?

Best Answer

Remove the line:

Redirect 404 /

And use your original:

ErrorDocument 404  https://myserver/wordpress/wp-content/themes/Theme1/404.php
Related Topic