The Stack Overflow podcast is back! Listen to an interview with our new CEO.
1 of 11

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
    </Directory>

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

# nano /var/www/html/.htaccess

    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} !^healthprocesspartners\.com$ [NC]
    RewriteRule ^(.*)$ http://greenriverit.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, but there are NO LOGS at nano /var/log/httpd/olddomain_com_error.log.

In case it was a permissions error, root typed chown apache:apache /var/www/html/.htaccess, but the same problem is persisting.

Is this an SELinux problem? Or is the syntax of the .htaccess file incorrect? Is there some other possible cause?

This is the first time that .htaccess has been used on this specific server, so there are no previous working conditions for .htaccess to which to refer on this server.