Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jetty-users] RewriteRegexRule /([0-9]+)$ to /player-$1 does not work, returns 404

Hello dear Jetty users,

I would like to add a rewrite rule which would rewrite requests to numeric URLs like https://slova.de/5 to https://slova.de/player-5 i.e. insert the "player-" string before the matched number.

After consulting http://www.eclipse.org/jetty/documentation/9.4.x/rewrite-handler.html I have added the rewrite module by running:

    java -jar /usr/share/java/jetty-distribution-9.4.6.v20170531/start.jar jetty.home=/usr/share/java/jetty-distribution-9.4.6.v20170531 jetty.base=/var/www/jetty-base --create-startd --add-to-start=http,servlet,webapp,deploy,resources,fcgi,websocket,proxy-protocol,rewrite

And then in the file $JETTY_BASE/start.d/rewrite.ini I have uncommented the rewritePathInfo line:

    ## Whether to rewrite the path info
    jetty.rewrite.rewritePathInfo=true

Finally I have added the section to the file $JETTY_BASE/webapps/slova.de.xml

    <Ref refid="Rewrite">
            <Call name="addRule">
                    <Arg>
                            <New class="org.eclipse.jetty.rewrite.handler.RewriteRegexRule">
                                    <Set name="regex">/([0-9]+)$</Set>
                                    <Set name="replacement">/player-$1</Set>
                            </New>
                    </Arg>
            </Call>
    </Ref>

Unfortunately I get 404 Not found when requesting URLs like https://slova.de/5

Below is my $JETTY_BASE/webapps/slova.de.xml in its entirety, please help me :-)

Regards
Alex

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN"
    "http://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure class="org.eclipse.jetty.servlet.ServletContextHandler">

    <New id="root" class="java.lang.String">
        <Arg>/var/www/html/slova.de</Arg>
    </New>

    <Set name="contextPath">/</Set>

    <Set name="virtualHosts">
            <Array type="java.lang.String">
                    <Item>slova.de</Item>
                    <Item>www.slova.de</Item>
            </Array>
    </Set>

    <Set name="resourceBase"><Ref refid="root" /></Set>

    <Set name="welcomeFiles">
        <Array type="string">
                <Item>index.php</Item>
                <Item>index.html</Item>
        </Array>
    </Set>

    <Ref refid="Rewrite">
            <Call name="addRule">
                    <Arg>
                            <New class="org.eclipse.jetty.rewrite.handler.RewriteRegexRule">
                                    <Set name="regex">/([0-9]+)$</Set>
                                    <Set name="replacement">/player-$1</Set>
                            </New>
                    </Arg>
            </Call>
    </Ref>

    <Call name="addFilter">
        <Arg>org.eclipse.jetty.fcgi.server.proxy.TryFilesFilter</Arg>
        <Arg>/*</Arg>
        <Arg>
            <Call name="of" class="java.util.EnumSet">
                <Arg><Get name="REQUEST" class="javax.servlet.DispatcherType" /></Arg>
            </Call>
        </Arg>
        <Call name="setInitParameter">
            <Arg>files</Arg>
            <Arg>$path /index.php?p=$path</Arg>
        </Call>
    </Call>

    <Call name="addServlet">
        <Arg>
            <New class="org.eclipse.jetty.servlet.ServletHolder">
                <Arg>default</Arg>
                <Arg>
                    <Call name="forName" class="java.lang.Class">
                        <Arg>org.eclipse.jetty.servlet.DefaultServlet</Arg>
                    </Call>
                </Arg>
                <Call name="setInitParameter">
                    <Arg>dirAllowed</Arg>
                    <Arg>false</Arg>
                </Call>
                <Call name="setInitParameter">
                    <Arg>gzip</Arg>
                    <Arg>true</Arg>
                </Call>
            </New>
        </Arg>
        <Arg>/</Arg>
    </Call>

    <Call name="addServlet">
        <Arg>org.eclipse.jetty.fcgi.server.proxy.FastCGIProxyServlet</Arg>
        <Arg>*.php</Arg>
        <Call name="setInitParameter">
            <Arg>proxyTo</Arg>
            <Arg>http://localhost:9000</Arg>
        </Call>
        <Call name="setInitParameter">
            <Arg>prefix</Arg>
            <Arg>/</Arg>
        </Call>
        <Call name="setInitParameter">
            <Arg>scriptRoot</Arg>
            <Arg><Ref refid="root" /></Arg>
        </Call>
        <Call name="setInitParameter">
            <Arg>scriptPattern</Arg>
            <Arg>(.+?\\.php)</Arg>
        </Call>
    </Call>

    <Call name="insertHandler">
        <Arg>
            <New id="RequestLog" class="org.eclipse.jetty.server.handler.RequestLogHandler">
                <Set name="requestLog">
                    <New id="RequestLogImpl" class="org.eclipse.jetty.server.NCSARequestLog">
                        <Set name="filename">/var/log/jetty/slova.de/yyyy_mm_dd.request.log</Set>
                        <Set name="filenameDateFormat">yyyy_MM_dd</Set>
                        <Set name="LogTimeZone">GMT</Set>
                        <Set name="retainDays">30</Set>
                        <Set name="append">true</Set>
                    </New>
                </Set>
            </New>
        </Arg>
    </Call>

</Configure>


Back to the top