logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in  RSS Apache Lounge  


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.


Post new topic   Forum Index -> Apache View previous topic :: View next topic
Reply to topic   Topic: How to write RewriteRule ?
Author
Mox



Joined: 14 Sep 2021
Posts: 5

PostPosted: Tue 14 Sep '21 18:06    Post subject: How to write RewriteRule ? Reply with quote

I need to write RewriteRule like this:

if it starts with /MyApp then /MyApp should be erased.

For example (original -> rewrited):
/MyApp/ -> /
/MyApp/Dir/file.html -> /Dir/file.html

How to do this?
Back to top
mraddi



Joined: 27 Jun 2016
Posts: 133
Location: Schömberg, Baden-Württemberg, Germany

PostPosted: Tue 14 Sep '21 19:27    Post subject: Reply with quote

Hello Mox,

I have placed the following two lines into a .htaccess located in my document-root:
Code:
RewriteEngine On
RewriteRule ^MyApp/(.*) /$1 [L]

In addition I have ensured that this .htaccess is processed by Apache and that the mod_rewrite-module is loaded.
Now if I access https://localhost/MyApp/phpinfo.php I get the same result as https://localhost/phpinfo.php.
If you prefer to be redirected to the new location the following modification is for you
Code:
RewriteEngine On
RewriteRule ^MyApp/(.*) /$1 [R=302,L]


Best regards
Matthias Smile
Back to top
Mox



Joined: 14 Sep 2021
Posts: 5

PostPosted: Tue 14 Sep '21 20:28    Post subject: Reply with quote

Thanks for that, but it didn't solve my problem.
Let me explain what exactly I am trying to do.

I have mapped subdomain to Tomcat webapp, and configured Apache to act as a proxy server like this:
Code:

<VirtualHost *:80>
    ServerName app.mydomain.com
    RewriteEngine On
    ProxyPass / http://localhost:8080/MyApp/
    ProxyPassReverse / http://localhost:8080/MyApp/
</VirtualHost>


so that I can open webapp in browser by using just subdomain URL app.mydomain.com

That works, and webapp is opening, but problem is that links on the pages are absolute, like /MyApp/images/someimage.jpg, and they have to be rewrited like this /images/someimage.jpg.

I thought by placing RewriteRule before ProxyPass would solve the issue, but it hasn't.
I tried what you suggested like this:

Code:

<VirtualHost *:80>
    ServerName app.mydomain.com
    RewriteEngine On
    RewriteRule ^MyApp/(.*) /$1 [L]
    ProxyPass / http://localhost:8080/MyApp/
    ProxyPassReverse / http://localhost:8080/MyApp/
</VirtualHost>


but links are not rewrited, as I checked Tomcat logs.

Can you help me solve this issue?
Back to top
mraddi



Joined: 27 Jun 2016
Posts: 133
Location: Schömberg, Baden-Württemberg, Germany

PostPosted: Tue 14 Sep '21 22:31    Post subject: Reply with quote

Good evening,

hopefully now I have understood the problem Smile

One idea is to add another ProxyPass for MyApp so the absolute links including the MyApp still work
Code:
<VirtualHost *:80>
    ServerName app.mydomain.com
    ProxyPass /MyApp/ http://localhost:8080/MyApp/
    ProxyPassReverse /MyApp/ http://localhost:8080/MyApp/
    ProxyPass / http://localhost:8080/MyApp/
    ProxyPassReverse / http://localhost:8080/MyApp/
</VirtualHost>

or use mod_rewrite with a redirect as in the following snippet
Code:
<VirtualHost *:80>
    ServerName app.mydomain.com
    RewriteEngine on
    RewriteRule ^/MyApp/(.*) /$1 [R=302,L]
    ProxyPass / http://localhost:8080/MyApp/
    ProxyPassReverse / http://localhost:8080/MyApp/
</VirtualHost>

to redirect the client to the URL without the MyApp
or use the PT-flag as shown here - otherwise it will result in a 404-not-found
Code:
<VirtualHost *:80>
    ServerName app.mydomain.com
    RewriteEngine on
    RewriteRule ^/MyApp/(.*) /$1 [PT]
    ProxyPass / http://localhost:8080/MyApp/
    ProxyPassReverse / http://localhost:8080/MyApp/
</VirtualHost>
Back to top
tangent



Joined: 16 Aug 2020
Posts: 125
Location: UK

PostPosted: Tue 14 Sep '21 23:12    Post subject: Reply with quote

@mraddi beat me to my reply, and suggests a different approach, but I'll post what I'd composed.

@Mox - your follow-up post explains more clearly what you're trying to achieve.

As you've found out, mod_rewrite manipulates requests, not responses, so the response bodies from Tomcat need editing with an output filter to edit the embedded URI's / URL's, before being passed back to the requestor.

In the past I've solved this proxy to Tomcat problem with either mod_substitute or mod_proxy_html. It rather depends on the complexity of the responses from Tomcat, but I'd say mod_proxy_html is the more flexible and robust method. Both will take a bit of setting up and testing.

I'd normally use a location block to encapsulate the proxy directives to the Tomcat server, even though in this case you've decided to proxy the entire virtual host.

A few other points to consider:

If your content from Tomcat is compressed, you'll need to disable that so the output filter can see the uncompressed responses. Alternatively, you can add a "RequestHeader unset Accept-Encoding" directive to your configuration, but you'll then find it more challenging to compress the final response back to your clients.

Next, if your Tomcat server is remote to the proxy server, you'll need to edit or remove the host part of embedded URL's to match your Apache site URL. I'd personally edit these to be site relative, even for a local tomcat server, e.g. /image.png rather than http(s)://tomcat.domain.com/image.png

If your Apache server ever ends up behind a load balancer, or the service URL differs from your Apache ServerName, then you'll probably need to edit the response headers too. Again, I'd go for site relative, e.g.
Code:
Header always edit Content-Location http(s|)://(app1.domain.com|app2.domain.com)/(.*) /$3
Header always edit Location http(s|)://(app1.domain.com|app2.domain.com)/(.*) /$3

Welcome to the complex world of filters!
Back to top
mraddi



Joined: 27 Jun 2016
Posts: 133
Location: Schömberg, Baden-Württemberg, Germany

PostPosted: Wed 15 Sep '21 6:03    Post subject: Reply with quote

Good morning Smile

@tangent: you are absolutely right - the clean way would be to modify the response instead of the request.
But it is easier and should be faster most times to modify the requests (compared to modifying the response) with the downside that the /MyApp/-path is not hidden but allowed as well.

Quote:
Welcome to the complex world of filters!

Oh yes - I couldn't agree more!
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 6998
Location: Germany, Next to Hamburg

PostPosted: Wed 15 Sep '21 8:35    Post subject: Reply with quote

Only to say there is: mod_proxy_html[1]. That can be an option, too.



[1] https://httpd.apache.org/docs/2.4/mod/mod_proxy_html.html
Back to top
Mox



Joined: 14 Sep 2021
Posts: 5

PostPosted: Wed 15 Sep '21 11:33    Post subject: Reply with quote

Thanks for your suggestions.

This solution that mraddi posted works:

Code:

<VirtualHost *:80>
    ServerName app.mydomain.com
    RewriteEngine on
    RewriteRule ^/MyApp/(.*) /$1 [PT]
    ProxyPass / http://localhost:8080/MyApp/
    ProxyPassReverse / http://localhost:8080/MyApp/
</VirtualHost>


I will have to test more to verify that there aren't further issues, but initialy works ok.
Back to top


Reply to topic   Topic: How to write RewriteRule ? View previous topic :: View next topic
Post new topic   Forum Index -> Apache