Lucky Slevin
Lucky Slevin

Reputation: 1

Apache ProxyPass only if specific string matchs in subdomain

I use apache as reverse proxy, is there any way to ProxyPass only if my subdomain match a specific string, something like :

<VirtualHost *:80> ServerAlias *.example.com <If "%{HTTP_HOST} -strmatch 'hello*.example.com'"> ProxyPreserveHost On ProxyPass "/" "http://192.168.1.22/" ProxyPassReverse "/" "http://192.168.1.22/" </If> </VirtualHost>

Its not possible to use ProxyPass inside "If", is there any work arround to achieve the same goal ?

thanks !

Upvotes: 0

Views: 1727

Answers (2)

W4rlock
W4rlock

Reputation: 176

I would try it with mod_rewrite and the Proxy directive.

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

Upvotes: 1

TheVanD
TheVanD

Reputation: 9

I think you can use either Regex or RewriteCond if it's possible and I'll explain my solution for this problem :

Since you want the same exact string to match, you can exclude everything except that string you're looking for, I've written for you a RewriteCond if it's usable :

RewriteEngine on
RewriteCond %{HTTP_HOST} ^.*\.example\.com[NC]
RewriteCond %{HTTP_HOST} !=hello.example.com[NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

This code should work, if it doesn't I'll search a regex condition to make the same pattern with different method, excluding every subdomain except the one that matches with hello.example.com

I have also this other code

RewriteCond %{HTTP_HOST} (^|\.)example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^(hello)\.example\.com$ [NC]
RewriteRule ^ http://hello.example.com%{REQUEST_URI} [NE,R=301,L]

Upvotes: 0

Related Questions