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.

  • IncludeOptional conf.d/*.conf that's not complete httpd configuration – Jasen Nov 11 '15 at 3:29
  • Is there anything in the error logs that might be relevant? – garethTheRed Nov 11 '15 at 7:36
  • As you're expecting olddomain to use .htaccess to redirect to new domain and it isn't, then I'd start by looking in the error log for olddomain. Hopefully, there will be something useful in there for us to work with. – garethTheRed Nov 11 '15 at 19:41
  • Have a look in apache's main log: /var/log/httpd/error.log. – garethTheRed Nov 12 '15 at 11:47
  • 1
    As you figured out the answer yourself, then maybe you should write your own answer to the question and let users who find it useful give you the credit. I won't be offended at all if you un-accept (is that a word?) my answer :-) – garethTheRed Nov 14 '15 at 10:28

I'm going to put my head above the parapet with this answer - after all, an answer is deletable!

Apache processes it's modules in a not very well defined order. You can view the processing order by enabling the mod_info module. On a Fedora 22 installation, this came up with the following (extact):

Post-Read Request: 
   -10 mod_http2.c 
   00 mod_headers.c 
   00 mod_remoteip.c 
   00 mod_proxy.c 
   10 mod_auth_digest.c 
   10 mod_http2.c 
   10 mod_reqtimeout.c 
   10 mod_setenvif.c 
   10 mod_unique_id.c 
Header Parse: 
   10 mod_setenvif.c 
HTTP Scheme: 
   30 http_core.c 
Default Port: 
   30 http_core.c 
Quick Handler: 
   00 mod_cache.c 
   00 mod_lua.c 
Translate Name: 
   -1 mod_lua.c 
   00 mod_rewrite.c 
   00 mod_proxy.c 
   00 mod_proxy_express.c 
   10 mod_alias.c 
   10 mod_userdir.c 
   10 mod_vhost_alias.c 
   10 mod_lua.c 
   21 mod_lua.c 
   30 core.c 

You'll notice that mod_proxy.c is first in the list.

My (very limited) understanding is that your proxy settings will be processed first, at which point your WAR files are returned and therefore the rewrite never gets applied.

There are ways to proxy using just mod_rewrite and the [P] flag as explained in the apache documentation here. It may be useful in this scenario.

| improve this answer | |
  • Your comments ultimately led me to the answer, which I added to the end of my OP. I am marking yours as accepted because it led me ultimately to something that works. Thank you and +1. – CodeMed Nov 14 '15 at 1:04

You need to enable module "rewrite" to get .hatccess working:

a2enmod rewrite
service httpd restart
| improve this answer | |
  • On CentoOS 7, you command gives -bash: a2enmod: command not found – CodeMed Nov 11 '15 at 19:06
  • @CodeMed Sorry, my bad! I'm a debian guy. – Saad Ismail Nov 12 '15 at 7:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.