Apache rewrite rule for html5 pushstate with proxy and exclusions

别说谁变了你拦得住时间么 提交于 2019-12-12 00:46:08

问题


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:


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/


来源:https://stackoverflow.com/questions/37167308/apache-rewrite-rule-for-html5-pushstate-with-proxy-and-exclusions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!