Redirect missing pages (404) to higher up same directory, Apache.

<Directory /var/www/html/mydirectory>
                RewriteEngine On
                RewriteCond $1 ([^/]+)/optional/(.*)
                RewriteCond /var/www/html/mydirectory/$1 !-f
                RewriteCond /var/www/html/mydirectory/$1 !-d
                RewriteCond /var/www/html/mydirectory/%1/optional/ -d
                RewriteRule (.*) "https://example.com/mydirectory/%1/optional/" [R=302,L]
</Directory>
                

Eg:

https://example.com/mydirectory/0101/optional/x/y/z/non-existant-file.html 

redirects to

https://example.com/mydirectory/0101/optional/

1 thought on “Redirect missing pages (404) to higher up same directory, Apache.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s