Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache mod_rewrite internally to different port

Is it possible to internally redirect (so url won't change in address bar) with mod_rewrite to different port on same host? Eg

http://host.com:8080 -> http://host.com:9999/myapplication/?param=val
like image 201
karolkpl Avatar asked Feb 16 '12 08:02

karolkpl


2 Answers

1, Enable mod_proxy

LoadModule  proxy_module         modules/mod_proxy.so
LoadModule  proxy_http_module    modules/mod_proxy_http.so

2, You should configure apache for vhost :

<VirtualHost *:8080>
    ....
    ProxyPass / http://host.com:9999/myapplication/?param=val
    ProxyPassReverse / http://host.com:9999/myapplication/?param=val

</VirtualHost>

3, Setup also VHost on port 9999

More info here:

  • http://httpd.apache.org/docs/2.2/mod/mod_proxy.html
  • http://www.apachetutor.org/admin/reverseproxies
like image 187
rkosegi Avatar answered Sep 21 '22 06:09

rkosegi


Elaboration on the mod_proxy solution with [P], the proxy flag:

  1. Enable modules mod_proxy and mod_proxy_http:

    a2enmod proxy proxy_http
    

    Without these two enabled, you'd later get a 300 Forbidden status and the error message "AH00669: attempt to make remote request from mod_rewrite without proxy enabled" in the logs.

  2. Place the following into the Apache2 vhost config section for the forwarding host:

    <VirtualHost *:8080>
      …
      RewriteEngine on
      RewriteCond  %{REQUEST_URI}  !^$
      RewriteCond  %{REQUEST_URI}  !^/
      RewriteRule  .*              -    [R=400,L]
    
      RewriteRule  (.*)  http://host.com:9999/myapplication/$1?param=val  [P,L]
      …
    </VirtualHost>
    

    This includes a technique by Steve Webster to prevent malicious URL crafting, explained here. Not sure how to deal with appending the GET parameter in this context, though.

  3. Restart Apache2:

    sudo service apache2 restart
    
like image 26
tanius Avatar answered Sep 22 '22 06:09

tanius