Apache rewrite rule for html5 pushstate with proxy

2019-09-06 11:19发布

I am using apache as http server, tomcat8 as application server (rest) and express as angular2 static server.

Following is my virtual host element

<VirtualHost *:80>
    ... other things - name path and logs ...


    ProxyPass /rest/ http://localhost:8080/rest/
    ProxyPassReverse /rest/ http://localhost:8080/rest/

    ProxyPass / http://localhost:3000/
    ProxyPassReverse / http://localhost:3000/

</VirtualHost>

Basically what I am doing is, whenever someone hits http://localhost he should see static web app which is hosted on http://localhost:3000 and when someone (web application) make request for http://localhost/rest, request should reach to tomcat8 on 8080 for rest call.

This is working properly.

Now what I want

I am using express static server which serves angular2 application. This application uses html5 pushState as router. Whenever user refreshes the page, it shows not found error.

I want redirection rule which redirect as following

http://localhost/somepath => http://localhost?redirect=/somepath

http://localhost/rest => (do not rewrite but proceed to proxy) http://localhost:8080/rest

I am not getting how to use rewrite rule here.

RewriteEngine  on
RewriteCond %{REQUEST_URI} !(/rest/)
RewriteCond "%{QUERY_STRING}" "!(.*(?:^|&))_redirect=([^&]*)&?(.*)&?$"
RewriteRule "^/?(.*)"        "http://localhost?_redirect=$1" [L,R,NE,QSA]

Above is my code for same, but it is not working. Can anyone suggest me proper condition and rule?

1条回答
够拽才男人
2楼-- · 2019-09-06 11:29

This is what I end up doing

    RewriteEngine  on
    // exception for everything which is on the webapp but has physical path
    RewriteCond %{REQUEST_URI} !(/rest/)
    RewriteCond %{REQUEST_URI} !(/bootstrap-template/)
    RewriteCond %{REQUEST_URI} !(/lib/)
    RewriteCond %{REQUEST_URI} !(/app/)
    RewriteCond %{REQUEST_URI} !(/assets/)
    RewriteCond %{REQUEST_URI} !(/browser-sync/)    
    RewriteCond %{REQUEST_URI} !(/node_modules/)    
    RewriteCond %{REQUEST_URI} !(/styles.css)$
    RewriteCond %{REQUEST_URI} !(/Helvetica.otf)$
    // exceptions end

    RewriteCond %{REQUEST_URI} (/)(.+)$
    RewriteCond "%{QUERY_STRING}" "!(.*(?:^|&))_redirect=([^&]*)&?(.*)&?$"
    RewriteRule "^/(.*)"        "http://localhost?_redirect=$1" [L,R,NE,QSA]

    ProxyPass /pixie/ http://localhost:8080/pixie/
    ProxyPassReverse /pixie/ http://localhost:8080/pixie/

    ProxyPass / http://localhost:3000/
    ProxyPassReverse / http://localhost:3000/
查看更多
登录 后发表回答