firstkungz
firstkungz

Reputation: 21

apache proxypass from subdomain

I want to proxypass in apache2 from subdomain to other ports like this

http://test1.example.com -> http://test1.example.com:6543

Is there anyway to write config file with not specific with this subdomain but all of the subdomain

in anyway like http://.*.example.com -> http://.*.example.com:6543

and http://.*.example.com/first/second -> http://.*.example.com:6543/first/second

Here is my config that

<VirtualHost *:80>
ServerName example.com
ProxyPassMatch ^([^.]+)\.example\.com(.*) http://$1.example.com:6543/$2
ProxyPassReverse /  http://example.com
</VirtualHost>

Upvotes: 2

Views: 1229

Answers (1)

Adam Katz
Adam Katz

Reputation: 16118

You'll need mod_rewrite for this. It can proxy with [P]. Use it as follows:

<VirtualHost *:80>
ServerName example.com
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^(.*) http://%1.example.com:6543/$1 [P]
</VirtualHost>

%1 is the subdomain, matched in the condition, $1 is the path, matched by the rule.


You can also do this without the RewriteCond, using just

<VirtualHost *:80>
RewriteRule ^(.*) http://%{HTTP_HOST}:6543/$1 [P]
</VirtualHost>

I used the extra step above to make it more informative, e.g. in the event you wanted to use the subdomain in the path.

Upvotes: 3

Related Questions