mod_rewrite rule to redirect root separate from everything else

Took me a bit to get this working and maybe someone else will find this useful.

In our case we have a on-premise service being moved to the cloud. To prevent an outage and not break any links for the first month or so after the migration a new domain has been chosen to.

Example URLs:

  • https://onprem.pickysysadmin.ca
    • This should forward to the root of the new service (https://cloudservice.pickysysadmin.ca/)
  • https://onprem.pickysysadmin.ca/a_bunch_of_stuff/more_stuff
    • This should redirect to the specified location on the old service (https://onprem.pickysysadmin.ca/)

Right now DNS looks like this:

  • onprem.pickysysadmin.ca -> 10.0.0.1
  • cloudservice.pickysysadmin.ca -> 10.0.0.2
  • apache_redirect_server.pickysysadmin.ca -> 10.0.0.3

Because onprem.pickysysadmin.ca is vendor maintained we didn’t want to goof with it to much or we’d just put the rewrite rules on it and save us having to use a third server (apache_redirect_server.pickysysadmin.ca).

Our solution was:

  1. Created a new DNS alias called onprem-old.pickysysadmin.ca and pointed it at 10.0.0.1 (onprem.pickysysadmin.ca)
  2. Alter the Apache vhost configuration on onprem.pickysysadmin.ca so it would respond to onprem-old.pickysysadmin.ca
    ServerAlias onprem-old.pickysysadmin.ca
  3. Configured the following re-write rules on apache_redirect_server.pickysysadmin.ca
    RewriteEngine On
    RewriteCond %{HTTP_HOST} onprem\.pickysysadmin\.ca [NC]
    RewriteRule ^/$ https://cloudservice.pickysysadmin.ca/ [NC,R=301,L]
    RewriteRule ^(.*)$ https://onprem-old.pickysysadmin.ca/$1 [NC,R=301,L]
    
    
    The entire vhost entry looks like this:
    
    <VirtualHost 10.0.0.3:80>
        DocumentRoot www/onprem.pickysysadmin.ca
        ServerName onprem.pickysysadmin.ca
        ErrorLog logs/onprem.pickysysadmin.ca-error_log
        CustomLog logs/onprem.pickysysadmin.ca-access_log common
    
        RewriteEngine On
        RewriteCond %{SERVER_PORT} 80
        RewriteRule ^(.*)$ https://onprem.pickysysadmin.ca$1 [R,L]
    </VirtualHost>
    
    <VirtualHost 10.0.0.3:443>
        DocumentRoot www/onprem.pickysysadmin.ca
        ServerName onprem.pickysysadmin.ca
        ErrorLog logs/onprem.pickysysadmin.ca-error_log
        CustomLog logs/onprem.pickysysadmin.ca-access_log common
    
        RewriteEngine On
        RewriteCond %{HTTP_HOST} onprem\.pickysysadmin\.ca [NC]
        RewriteRule ^/$ https://cloudservice.pickysysadmin.ca/ [NC,R=301,L]
        RewriteRule ^(.*)$ https://onprem-old.pickysysadmin.ca/$1 [NC,R=301,L]
    
        SSLEngine on
        SSLHonorCipherOrder on
        SSLProtocol all -SSLv2 -TLSv1
        SSLCipherSuite ECDHE-RSA-AES128-SHA256:AES128-GCM-SHA256:RC4:HIGH:!MD5:!aNULL:!EDH:!EXPORT56:!ADH
        SSLCertificateFile <PATH TO CERT>
        SSLCertificateKeyFile <PATH TO CERT>
        SSLCertificateChainFile <PATH TO CERT>
    </VirtualHost>
  4. On migration day we changed onprem.pickysysadmin.ca to point to 10.0.0.3 (apache_redirect_server.pickysysadmin.ca)

Not overly complicated but it took me a while to figure out how to get the rewrite rules working properly.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.