Search code examples
apachemod-rewritedirectorysubdomain

apache mod_rewrite subdomain to dir


I have 4 servers, and I want to merge them with a domain. For example, what I am doing now:

I want to

If any one access www.example.org/games/

I want to show in the browser www.example.org/games/ virtually but physically it files will be hosted in the games.example.org server?


Solution

  • Pure mod_rewrite rules will not be enough, since they require mod_proxy as well to make the subrequests work correctly. Especially if you want all the absolute links to be auto rewritten as the page is served back. If you have full access to Apache, this code can be used in the virtualhost section of your www.example.org domain:

    <Location  /games>
            ProxyPassReverse http://games.example.org
            ProxyPassReverse http://games.example.org:80
    
            RewriteEngine On
            RewriteRule games(.*)$ http://games.example.org/$1 [QSA,P,L]
    </Location>
    

    The last RewriteRule is really what you need, but the "P" flag will likely not work unless you can load mod_proxy as well. Make sure to load mod_proxy with the most restrictive settings and allow proxy requests though the ProxyPass directives.

    Also ProxyPassReverse will NOT work in .htaccess. It has to be in the Apache vhost config itself.

    Hope that helps!