skip to Main Content

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 !

2

Answers


  1. 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]
    
    Login or Signup to reply.
  2. 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>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search