简体   繁体   中英

Why is apache rewriting the URL twice?

I am having a scenerio, in which there is 3 servers:

  1. Apache (web server) on port 8080
  2. Tomcat (Application server) on port 8083
  3. NodeJs server (for url prefetch) on port 8084

My requirement is, if the request comes to 8080, redirect all requests to 8083, except the ones which has "_escaped_fragment_=" in it.

So i wrote two proxies as

<IfModule mod_proxy_http.c>
  ProxyPass /makeSnapshot http://[IP]:8084
  ProxyPassReverse /makeSnapshot http://[IP]:8084
</IfModule>
<IfModule mod_proxy_http.c>
  ProxyPass /site http://[IP]:8083
  ProxyPassReverse /site http://[IP]:8083
</IfModule>

Now I have written some rewrite rules directly in the configuration file as follows:

<IfModule mod_rewrite.c>
    RewriteEngine On
        LogLevel alert rewrite:trace3
    RewriteBase /
    RewriteRule ^.*lib/(.*)$ /site/lib/$1 [NC,L]
    RewriteRule ^.*uploaded_files/(.*)$ /site/uploaded_files/$1 [NC,L]
    RewriteRule ^.*assets/(.*)$ /site/assets/$1 [NC,L]
    RewriteRule ^.*app/(.*)$ /site/app/$1 [NC,L]

    RewriteCond %{QUERY_STRING} ^.*_escaped_fragment_=(.*)
    RewriteRule ^.*$ /makeSnapshot?url=%1 [B,P,L,S=1]

    RewriteRule ^(.*)$ /site/$1 [L,P]
</IfModule>

My expectation was that all the server calls will be redirected to tomcat including the static files, since I mentioned it in the rewrite rules.

The node server is to generate the static page querying from tomcat server, and after parsing the javascript.

Now the issue is, all the redirection happens just fine, but when the page is rendered from nodejs server, it agains redirects to tomcat automatically.

I tried many approaches to resolve this, but am not sure why apache redirects it twice.

Help is really appreciated.

When you use the P flag, that is as if you are calling a ProxyPass . So you don't need the ProxyPass lines in your vhost config (those directives won't work in htaccess anyways) and try this:

<IfModule mod_rewrite.c>
    RewriteEngine On
        LogLevel alert rewrite:trace3
    RewriteBase /
    RewriteRule ^.*lib/(.*)$ /site/lib/$1 [NC,L]
    RewriteRule ^.*uploaded_files/(.*)$ /site/uploaded_files/$1 [NC,L]
    RewriteRule ^.*assets/(.*)$ /site/assets/$1 [NC,L]
    RewriteRule ^.*app/(.*)$ /site/app/$1 [NC,L]

    RewriteCond %{QUERY_STRING} ^.*_escaped_fragment_=(.*)
    RewriteRule ^.*$ http://[IP]:8084/?url=%1 [B,P,L,S=1]

    RewriteRule ^(.*)$ http://[IP]:8083/$1 [L,P]
</IfModule>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM