The Stack Overflow podcast is back! Listen to an interview with our new CEO.
7 of 11 added 82 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
    </Directory>

# 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, 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.

The complete text of httpd.conf is:

ServerRoot "/etc/httpd"
Listen 80
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin root@localhost

<Directory />
    AllowOverride none
    Require all denied
</Directory>

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

<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>

<Files ".ht*">
    Require all denied
</Files>

ErrorLog "logs/error_log"
LogLevel warn

<IfModule log_config_module>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common

    <IfModule logio_module>
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>

    CustomLog "logs/access_log" combined
</IfModule>

<IfModule alias_module>
    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>

<IfModule mime_module>
    TypesConfig /etc/mime.types
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml
</IfModule>

AddDefaultCharset UTF-8

<IfModule mime_magic_module>
    MIMEMagicFile conf/magic
</IfModule>

EnableSendfile on
IncludeOptional conf.d/*.conf

EDIT:


Per @Jasen's request, I typed the following:

[root@hostname html]# nano /etc/httpd/conf.d/
[root@hostname conf.d]# ls -al
total 40
drwxr-xr-x. 2 root root 4096 Oct  6 19:43 .
drwxr-xr-x. 8 root root 4096 Dec 23  2014 ..
-rw-r--r--. 1 root root 2893 Jul 23  2014 autoindex.conf
-rw-r--r--  1 root root 2110 Nov  3 19:25 hostname.com.conf
-rw-r--r--. 1 root root  366 Jul 23  2014 README
-rw-r--r--. 1 root root 9426 Jul 23  2014 ssl.conf
-rw-r--r--. 1 root root 1252 Jul 23  2014 userdir.conf
-rw-r--r--  1 root root  821 Oct  5 14:48 welcome.conf
[root@hostname conf.d]# nano /etc/httpd/conf.d/hostname.com.conf

The resulting contents are:

<VirtualHost *:443>
    ServerName www.hostname.com
    ServerAlias hostname.com
    ErrorLog /var/log/httpd/hostname_com_error.log
    CustomLog /var/log/httpd/hostname_com_requests.log combined
    SSLEngine on
    SSLProxyEngine on
    SSLCertificateFile /etc/pki/tls/certs/localhost.crt
    SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
    ProxyPass / ajp://server.ip.addr:8009/
    ProxyPassReverse / ajp://server.ip.addr:8009/
</VirtualHost>

Listen 444

<VirtualHost *:444>
    ServerName www.hostname.com
    ServerAlias hostname.com
    ErrorLog /var/log/httpd/hostname_com_error.log
    CustomLog /var/log/httpd/hostname_com_requests.log combined
    SSLEngine on
    SSLProxyEngine on
    SSLCertificateFile /etc/pki/tls/certs/localhost.crt
    SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
    ProxyPass / ajp://server.ip.addr:8010/
    ProxyPassReverse / ajp://server.ip.addr:8010/
</VirtualHost>

<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
    ProxyPass / ajp://server.ip.addr:8011/
    ProxyPassReverse / ajp://server.ip.addr:8011/
</VirtualHost>

<VirtualHost www.newdomain.com:80>
    ServerName www.newdomain.com
    ServerAlias newdomain.com
    ErrorLog /var/log/httpd/newdomain_com_error.log
    CustomLog /var/log/httpd/newdomain_com_requests.log combined
    ProxyPass / ajp://server.ip.addr:8012/
    ProxyPassReverse / ajp://server.ip.addr:8012/
</VirtualHost>

<VirtualHost www.anotherdomain.com:80>
    ServerName www.anotherdomain.com
    ServerAlias anotherdomain.com
    ErrorLog /var/log/httpd/anotherdomain_com_error.log
    CustomLog /var/log/httpd/anotherdomain_com_requests.log combined
    ProxyPass / ajp://server.ip.addr:8013/
    ProxyPassReverse / ajp://server.ip.addr:8013/
</VirtualHost>

Note that I am waiting to comment out the VirtualHost block for olddomain.com until I can get .htaccess to handle all the redirects of all possible url patterns in olddomain.com/everypossibleurl. For now, olddomain.com is serving up a war that has individual redirects from each and every old url in olddomain.com, which is very inefficient. I want .htaccess to do the job so that the server no longer has to serve up a war for olddomain.com.

As per @garethTheRed's suggestion, I recreated the trigger by typing olddomain.com/badlink in a client web browser, and then tried nano /var/log/httpd/olddomain_com_error.log but found that the file is completely empty. I next tried nano /var/log/httpd/olddomain_com_requests.log and found the following log record showing a 404:

client.ip.addr - - [11/Nov/2015:14:36:43 -0500] "GET /badlink HTTP/1.1" 404 1008 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0"

I am currently reading apache documentation about the rewrite rule at this link because of the following comment from david600130 that I read at this other link:

Using .htaccess files and setting AllowOverride All is not recommended. 
It's better to put the rewrite commands in your Directory section in  
httpd.conf. To enable the rewrite engine in this context, you need to  
set "RewriteEngine On" and "Options FollowSymLinks" must be enabled.  

If this is a better approach for CentOS 7, where do I put RewriteEngine On and Options FollowSymLinks?

Or perhaps there is a way to have the apache config do it without resorting to .htaccess?