301 redirection with URL exclusion problem

#1
Hi. I've been trying to redirect all URLs from an old domain to a new one, except for two specific pages (/contact-form and /about). Here is the code block I have added at the top of .htaccess file just before #Beging Wordpress:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)olddomain\.com$ [NC]
RewriteRule ^(contact-form|about)($|/) - [L]

# Exclude specific file extensions from redirection
RewriteCond %{THE_REQUEST} !/contact-form/?\s [NC]
RewriteCond %{REQUEST_URI} !\.(css|js|png|jpg|gif||webp|ico)$ [NC]
RewriteCond %{THE_REQUEST} !/about/?\s [NC]
RewriteCond %{REQUEST_URI} !\.(css|js|png|jpg|gif||webp|ico)$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]


Although the redirections take place, I encounter a 404 server error specifically on the excluded pages (olddomain.com/contact-form and /about) when visiting them:

404 Not Found
The resource requested could not be found on this server!

However, when I test the same .htaccess code block on localhost (Xampp, Apache server), it works well. Any help would be appreciated.
 
#2
I replaced RewriteEngine On with RewriteCond %{ENV:REDIRECT_STATUS} ^$ in the first line and the problem got soved. Here is the updated code block:


RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com [NC]
RewriteCond %{REQUEST_URI} !\.(css|js|png|jpg|gif|webp|ico)$ [NC]
RewriteCond %{REQUEST_URI} !^/contact-form [NC]
RewriteCond %{REQUEST_URI} !^/about [NC]
RewriteRule ^(.*)$ https://newdomain.com/$1 [R=301,L]
 
Top