AngularJS, Google App Engine and URLrewrite

438 views Asked by At

I have enabled the html5mode for an AngularJS app and have a question about configuring the URLrewrite. I referenced this post and realized that my issues is with the urlrewrite configuration.

My goal is to have all calls written back to the index.html so the angular router can route properly. Below is my existing configuration. I have tried many different types, but cannot seem to get the rewrite to work properly:

~~ urlrewrite.xml ~~

<urlrewrite default-match-type="wildcard">

    <rule>
        <from>/**</from>
        <to>/index.html</to>
    </rule>

    <outbound-rule>
        <from>/**</from>
        <to>/index.html</to>
    </outbound-rule> 

</urlrewrite>

Any pointers on what i'm doing wrong?

1

There are 1 answers

1
thilemann On

I know this is an quite old post, but since I have stumpled upon it while trying to achieve the same - I will provide an answer with my solution.

Provided that urlrewritefilter has been configured properly, add the following to urlrewrite.xml:

<urlrewrite>
    <rule>
        <condition type="request-uri" operator="notequal">^/$</condition>
        <condition type="request-uri" operator="notequal">^/angular/.*$</condition>
        <condition type="request-uri" operator="notequal">^/_ah/.*$</condition>
        <condition type="request-uri" operator="notequal">^/api/.*$</condition>
        <condition type="request-uri" operator="notequal">^/bower_components/.*$</condition>
        <condition type="request-uri" operator="notequal">^/css/.*$</condition>
        <condition type="request-uri" operator="notequal">^/fonts/.*$</condition>
        <condition type="request-uri" operator="notequal">^/img/.*$</condition>
        <condition type="request-uri" operator="notequal">^/js/.*$</condition>
        <condition type="request-uri" operator="notequal">^/tasks/.*$</condition>
        <condition type="request-uri" operator="notequal">^/test/.*$</condition>
        <condition type="request-uri" operator="notequal">^/vendor/.*$</condition>
        <condition type="request-uri" operator="notequal">^/index.html*$</condition>
        <to>/index.html</to>
    </rule>
</urlrewrite>

This rule will catch all requests ^(.*)$ as the "from" element is missing. Ofcourse you'll need to add routes specific to your application, but take notice that ^/api/.*$ is among the ignored routes, allowing your Google App Engine Endpoints to function properly.

When setting up the filter (default in web.xml), it is very useful to increase the default log level so you can look at what requests gets rewritten in the console:

<filter>
    <!-- Add this to your existing configuration for debug purposes -->
    <init-param>
        <param-name>logLevel</param-name>
        <param-value>TRACE</param-value>
    </init-param>
</filterr>