googlehhvm

Redirect custom domains to the Apache server

NOTE: We are in the process of modifying the file structure and configuration for many Bitnami stacks. On account of these changes, the file paths stated in this guide may change depending on whether your Bitnami stack uses native Linux system packages (Approach A), or if it is a self-contained installation (Approach B). To identify your Bitnami installation type and what approach to follow, run the command below:

 $ test ! -f "/opt/bitnami/common/bin/openssl" && echo "Approach A: Using system packages." || echo "Approach B: Self-contained installation."

The output of the command indicates which approach (A or B) is used by the installation, and will allow you to identify the paths, configuration and commands to use in this guide. Refer to the FAQ for more information on these changes.

When redirecting custom domains to the Apache server, there are two steps to be performed:

  • Create a virtual host for each domain (with certificates, if required)
  • Add redirection rules to the Apache server configuration

Approach A: Bitnami installations using system packages

Create a virtual host for each domain

To create a virtual host:

  • Add a ServerAlias in the /opt/bitnami/apache2/conf/vhosts/APPNAME-vhost.conf file for your application. This option is designed to specify alternate names for a host and is used when matching requests. Here’s an example:

    <VirtualHost *:80>
      ServerName app.example.com
      ServerAlias www.app.example.com app.example.org www.app.example.uk.org
      ...
    
  • Add a ServerAlias in the /opt/bitnami/apache2/conf/vhosts/APPNAME-https-vhost.conf file for your application. This option is designed to specify alternate names for a host and is used when matching requests. Here’s an example:

    <VirtualHost *:443>
      ServerName app.example.com
      ServerAlias www.app.example.com app.example.org www.app.example.uk.org
      ...
    

    You will also need to create new certificates for each virtual host running on port 443 or use existing certificates, such as the default certificates shipped with the stack. You have two options:

    * Option A: Update the *SSLCertificateFile* and *SSLCertificateKeyFile* directives for the virtual host to reflect the correct path for each virtual host's certificates:
    
    ...
    SSLCertificateFile "/opt/bitnami/apache2/conf/bitnami/certs/app.example.com.crt"
    SSLCertificateKeyFile "/opt/bitnami/apache2/conf/bitnami/certs/app.example.com.key"
    ...
    
    * Option B: Create symlinks to the default certificates.
    
  • After modifying the Apache configuration files, restart Apache to apply the changes:

    $ sudo /opt/bitnami/ctlscript.sh restart apache
    

Add redirection rules

The next step is to add the necessary redirection rules for each domain. The sections below cover two common scenarios.

Redirect www.myapp.example.com to myapp.example.com

This redirection is an SEO “best practice”.

  • Add the following rules in the /opt/bitnami/apache2/conf/vhosts/APPNAME-vhost.conf file, within the VirtualHost block for your domain:

    <VirtualHost *:80>
      RewriteEngine On
      RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
      RewriteRule ^(.*)$ http://%1$1 [R=permanent,L]
      ...
    
  • Add the following rules in the /opt/bitnami/apache2/conf/vhosts/APPNAME-https-vhost.conf file, within the VirtualHost block for your domain:

    <VirtualHost *:443>
      RewriteEngine On
      RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
      RewriteRule ^(.*)$ https://%1$1 [R=permanent,L]
      ...
    
  • After modifying the Apache configuration files, restart Apache to apply the changes:

    $ sudo /opt/bitnami/ctlscript.sh restart apache
    
Redirect myapp.example.com to www.myapp.example.com
  • Add the following in the /opt/bitnami/apache2/apps/APPNAME/conf/httpd-vhosts.conf file in the VirtualHost block for your domain. Or, to apply this redirection by default for all domains, add it to the default VirtualHost block in the /opt/bitnami/apache2/conf/bitnami.conf file.

    <VirtualHost *:80>
      RewriteEngine On
      RewriteCond %{HTTP_HOST} !^www\. [NC]
      RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
      ...
    
    <VirtualHost *:443>
      RewriteEngine On
      RewriteCond %{HTTP_HOST} !^www\. [NC]
      RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
      ...
    
  • After modifying the Apache configuration files, restart Apache to apply the changes.

Approach B: Self-contained Bitnami installations

Create a virtual host for each domain

To create a virtual host:

  • Add a ServerAlias in the /opt/bitnami/apps/APPNAME/conf/httpd-vhosts.conf file for your application. This option is designed to specify alternate names for a host and is used when matching requests. Here’s an example:

    <VirtualHost *:80>
      ServerName app.example.com
      ServerAlias www.app.example.com app.example.org www.app.example.uk.org
      ...
    
    <VirtualHost *:443>
      ServerName app.example.com
      ServerAlias www.app.example.com app.example.org www.app.example.uk.org
      ...
    

    You will also need to create new certificates for each virtual host running on port 443 or use existing certificates, such as the default certificates shipped with the stack. You have two options:

    * Option A: Update the *SSLCertificateFile* and *SSLCertificateKeyFile* directives for the virtual host to reflect the correct path for each virtual host's certificates:
    
    ...
    SSLCertificateFile "/opt/bitnami/apps/APPNAME/conf/certs/app.example.com.crt"
    SSLCertificateKeyFile "/opt/bitnami/apps/APPNAME/conf/certs/app.example.com.key"
    ...
    
    * Option B: Create symlinks to the default certificates.
    
  • Check that the /opt/bitnami/apache2/conf/bitnami/bitnami-apps-vhosts.conf file includes the httpd-vhosts.conf file for your application. It should include a line like the one below.

    Include "/opt/bitnami/apps/APPNAME/conf/httpd-vhosts.conf"
    

    If it does not, add the line above to the /opt/bitnami/apache2/conf/bitnami/bitnami-apps-vhosts.conf file, replacing the APPNAME placeholder with the correct directory name for your application.

  • After modifying the Apache configuration files, restart Apache to apply the changes:

    $ sudo /opt/bitnami/ctlscript.sh restart apache
    

Add redirection rules

The next step is to add the necessary redirection rules for each domain. The sections below cover two common scenarios.

Redirect www.myapp.example.com to myapp.example.com

This redirection is an SEO “best practice”.

  • Add the following rules in the /opt/bitnami/apps/APPNAME/conf/httpd-vhosts.conf file, within the VirtualHost block for your domain:

    <VirtualHost *:80>
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ http://%1$1 [R=permanent,L]
    ...
    
    <VirtualHost *:443>
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ https://%1$1 [R=permanent,L]
    ...
    
  • After modifying the Apache configuration files, restart Apache to apply the changes.

Redirect myapp.example.com to www.myapp.example.com
  • Add the following in the /opt/bitnami/apps/APPNAME/conf/httpd-vhosts.conf file in the VirtualHost block for your domain. Or, to apply this redirection by default for all domains, add it to the default VirtualHost block in the /opt/bitnami/apache2/conf/bitnami.conf file.

    <VirtualHost *:80>
      RewriteEngine On
      RewriteCond %{HTTP_HOST} !^www\. [NC]
      RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
     ...
    
    <VirtualHost *:443>
      RewriteEngine On
      RewriteCond %{HTTP_HOST} !^www\. [NC]
      RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
     ...
    
  • After modifying the Apache configuration files, restart Apache to apply the changes:

    $ sudo /opt/bitnami/ctlscript.sh restart apache
    
Last modification June 9, 2020