Search code examples
xmlurl-rewritingdnsjettyexist-db

URL rewriting issue in eXist-db - DNS A record - Jetty server


I have a DNS A record I want my app to be forwarded to. I have added to $EXIST_HOME/webapp/WEB-INF/controller-config.xml this line:

<root server-name="something.mydomain.com" pattern=".*" path="xmldb:exist:///db/apps/myapp"/>

This is what I have in my controller.xql regarding app-root:

else if (contains($exist:path,"app-root")) then
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
    <forward url="{$exist:controller}/{substring-after($exist:path, '$app-root/')}">
        <set-header name='Cache-Control' value="no"/>
    </forward>
</dispatch>

Now when I point my browser to something.mydomain.com I correctly see db/apps/myapp content (still working out how to get the index.html within that folder to kick in, but that's a secondary issue). something.mydomain.com/index.html transforms into 'http://something.mydomain.com/exist/index.html' and I see the page. Nevertheless I have lost all the links to images, CSS style, etc. Upon inspection, I see, for instance, that $app-root doesn't get unpacked (for instance, my banner retains the address '$app-root/resources/img/banner.jpg' in the HTML source of my page in the browser). I have changed that into /resources/img/banner.jpg, resources/img/banner.jpg, etc, and I haven't had any luck. Needless to say, it was all working perfectly before changing the $EXIST_HOME/webapp/WEB-INF/controller-config.xml. What am I missing?

Additional related question:

I haven't found <Set name="contextPath">/exist</Set> in $EXIST_HOME/tools/jetty/etc/jetty.xml as instructed in the eXist: A NoSQL Document Database and Application Platform book. Were can I find it, so that I can remove the 'exist' bit of the URL?


Solution

  • OK, that was partially due to my ignorance on the subject and the configuration of some of the files I wanted to serve. Since I had

    <root server-name="something.mydomain.com" pattern=".*" path="xmldb:exist:///db/apps/myapp/subfolder"/>
    

    in my $EXIST_HOME/webapp/WEB-INF/controller-config.xml, eXist-db was looking for a controller inside the /subfolder folder. The modules and configuration files (included he controller.xql) were in the myapp folder, so eXist-db was using that controller (the request for a controller fell back to the myapp folder, since there was no controller in the subfolder). Once I put a controller inside the subfolder, I edited this:

    else if (ends-with($exist:resource, ".html")) then
        (: the html page is run through view.xql to expand templates :)
        <dispatch xmlns="http://exist.sourceforge.net/NS/exist">
            <view>
                <forward url="../modules/view.xql"/>
            </view>
            <error-handler>
                <forward url="../error-page.html" method="get"/>
                <forward url="./modules/view.xql"/>
            </error-handler>
        </dispatch>
    

    So that I could still use the view.xql from myapp folder.

    This solved the problem only partially, though, because now $app-root doesn't get unpacked. For instance, if my css is in myapp/resources/css/ and is referenced from a file within the subfolder directory, it'll not be loaded. The only workaround I've found so far is that of hardcoding with the full http:// address every css, jpg, js, etc. referenced from pages in the subfolder. Is there a way to tell eXist-db to look one folder up the one specified in the URL rewriting rules? I've also tried with:

    else if (contains($exist:path,"app-root")) then
    <dispatch xmlns="http://exist.sourceforge.net/NS/exist">
        <forward url="xmldb:exist://db/apps/myapp">
            <set-header name='Cache-Control' value="no"/>
        </forward>
    </dispatch>
    

    , with two or three slashes after exist:, which doesn't do the trick (error 500), nor does forward url="http://fulladdress/db/apps/myapp".

    Also, now the

    else if ($exist:path eq "/") then
        (: forward root path to index.xql :)
        <dispatch xmlns="http://exist.sourceforge.net/NS/exist">
            <redirect url="index.html"/>
        </dispatch> 
    

    in the subfolder/controller.xql forwards correctly an empty $exist:path to index.html.

    Finally, I have managed to find where newer versions of eXist-db put the <Set name="contextPath">/exist</Set> code. It's in eXist-db/tools/jetty/webapps/exist-webapp-context.xml.