0

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?

  • 1
    Check out this answer from a question yesterday - you'll just need to change the actual URL it redirects to. https://webmasters.stackexchange.com/questions/107186/configure-apache-to-redirect-all-requests-to-index-html/107187?noredirect=1#comment136754_107187 – ivanivan Jun 21 '17 at 15:34
  • Duplicate of https://serverfault.com/questions/775570/apache-2-4-errordocument-for-multiple-subdomains/999619#999619 For answer see https://serverfault.com/a/999619/174375 – Thomas Lauria Jan 20 '20 at 09:09

1 Answers1

-1

Remove the line:

Redirect 404 /

And use your original:

ErrorDocument 404  https://myserver/wordpress/wp-content/themes/Theme1/404.php
moose-o
  • 101
  • 1
  • That's a good point - I think my wording was not clear. I don't want to redirect them to http://....404.php, I want everything to redirect to https://...404.php - *except* for attempts to access that error page itself. – FrustratedWithFormsDesigner Jun 21 '17 at 14:57
  • Sorry, I misunderstood. Try my edit. – moose-o Jun 21 '17 at 18:01
  • That doesn't seem to be doing anything. I think `ErrorDocument` needs to work with something that generates the error code. I was using `Redirect 404 /` to do that. Removing it just results in content from the next virtualhost on 443 to be returned. – FrustratedWithFormsDesigner Jun 21 '17 at 19:39