10 of 11 deleted 7865 characters in body

why isn't .htaccess redirecting requests on CentOS 7?

A web app running on CentOS 7 with Apache 2.4 recently changed its domain name from olddomain.com to newdomain.com. The structure of the app at the newdomain.com is different than on the olddomain.com, so all requests from the olddomain.com/anyurl need to be redirected to the root url newdomain.com.

A new .htaccess file was created, and httpd was restarted, so why do requests for olddomain.com/testbadurl fail to redirect to newdomain.com? Instead, the user just gets a 404 error at olddomain.com/testbadurl.

Here are the steps that were taken at the command line:

# nano /etc/httpd/conf/httpd.conf

    <Directory "/var/www/html">
        AllowOverride All

# sudo systemctl restart httpd
# cd /var/www/html

# nano /var/www/html/.htaccess

    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} !^olddomain\.com$ [NC]
    RewriteRule ^(.*)$ http://newdomain.com [R=301,L]

# systemctl restart httpd

Then the test user types olddomain.com/testbadlink in the web browser. The logs at nano /var/log/httpd/olddomain_com_requests.log show a 404 error.


@garethTheRed got me thinking about VirtualHost directives in the httpd conf files, which ultimately led me to develop the following answer, which works and now solves the problem:

<VirtualHost www.olddomain.com:80>
    ServerName www.olddomain.com
    ServerAlias olddomain.com
    ErrorLog /var/log/httpd/olddomain_com_error.log
    CustomLog /var/log/httpd/olddomain_com_requests.log combined
    RedirectMatch ^(.*)$ http://newdomain.com

Note that it is more secure to have the httpd conf files do this than to allow external access to an .htaccess file.