This post documents the process of setting up a reverse proxy to load one (Origin) website in the subdirectory of another. During this process the reverse proxy directives will also append HTML to the head and body of the origin website.

 

Prerequisites

The instructions below are based on an Apache Linux server running cPanel/ Web Host Manager. The set-up requires the following Apache modules:

  • mod_proxy
  • mod_proxy_http
  • od_include
  • mod_headers
  • mod_rewrite
  • mod_sed

mod_sed is the only uncommon module, however it is only required if you are using the OutputSed directives, if you don’t plan on making any modifications to the origin website you can skip it.

 

VirtualHost

The server will already be preconfigured to listen for additional .conf files for each domain once they’re added to the correct directories. For the standard WHM/cPanel/Apache combination:

/etc/apache2/conf.d/userdata/ssl/2_4/$user/$domainname/$file.conf

Where $user is the Linux user account associated with the hosting package, $domainname is your cPanel domain name and $file is the .conf filename.

/etc/apache2/conf.d/userdata/ssl/2_4/example/example.com/reverseproxy.conf

 

ProxyPass Directives

Inside reverseproxy.conf the Reverse proxy directives are added:

  • /subdirectory where the origin website will be displayed (could be root “/”)
  • https://origin-website.example.com/ is the origin website
ProxyPreserveHost off
SSLProxyEngine on
SSLProxyVerify off
SSLProxyCheckPeerCN off
SSLProxyCheckPeerName off
SSLProxyCheckPeerExpire off
ProxyPass "/subdirectory" "https://origin-website.example.com/"
ProxyPassReverse "/subdirectory" "https://origin-website.example.com/"

 

## Stop any existing rewrites on subdirectory
RewriteRule ^(subdirectory)($|/) - [L]

<Location "/subdirectory">

     <IfModule mod_headers.c>
        RequestHeader unset accept-encoding
        Header unset Content-Length
     </IfModule>

     # Add new layout fields
     Options +Includes
     AddOutputFilterByType Sed;INCLUDES text/html
     OutputSed "/<!--#[a-zA-z]/d"
     OutputSed "s/\\(<\/head>\\)/\\<!--#include virtual=\"\/layout\/head.inc.php\"--><\/head>/g"
     OutputSed "s/\\(<body[^>]*>\\)/\\<!--#include virtual=\"\/layout\/header.inc.php\"-->/g"
     OutputSed "s/\\(<\/body>\\)/\\<!--#include virtual=\"\/layout\/footer.inc.php\"--><\/body>/g"

</Location>

The above code will automatically try to include additional files in the pages processed by ProxyPass.

  • layout/head.inc.php will be appended to the <head>
  • layout/header.inc.php will be added to the start of <body> tag
  • layout/footer.inc.php will be added just before the </body> tag

These files can be used to apply style/scripting on top of the code supplied by the proxy.

 

Rebuild and Restart Apache

Once the reverse proxy directives have been added to the Apache configuration, the live conf file needs to be rebuilt to include the new directives:

/usr/local/cpanel/scripts/rebuildhttpdconf

The rebuild process will flag any errors with the reverse proxy code. These can then be rectified before a successful rebuild.

Then the obligatory Apache restart:

/usr/local/cpanel/scripts/restartsrv_httpd

 

#

Hello, I'm Keith, a website developer in Belfast, Northern Ireland working with PHP, Magento, Shopify and WordPress.

I've been building websites for over 10 years, from custom website development to bespoke web applications, Shopify and Magento ecommerce and Online Leaning Environments. I've worked on a range of projects and am always looking out for the next interesting project.

Related Notes Posts

Spam Investigation Toolkit

Tools to help identify and engage spammers. I'm often asked what tools are best to use to identify spammers, there are plenty of free tools... October 2022 · Notes · Spam

Lighthouse and Google Fonts

I recently had performance issues on Google Lighthouse due to the fonts used on the website. The Google Fonts I used were classified as a... July 2022 · Notes

Set-up a Rackspace CDN service

Quick guide for setting-up a CDN on Rackspace. ## CDN Set-up Log into your [Rackspace MyCloud](https://mycloud.rackspace.com/) account. Click into Storage > CDN then "Create Service".... May 2022 · Notes

More Notes Posts...