Search code examples
wordpresstomcaturl-rewritingphp-java-bridge

Pretty Permalinks not working - WordPress with Tomcat


Thanks to someone's odd requirements, I've managed to serve WordPress with Tomcat 9. Everything works fine with the help of PHP/Java Bridge.
However, I'm unable to follow any links from home page when pretty permalinks enabled in WordPress (when clicked on links it shows "No input file specified", plain URLs work fine though). I think the problem is to rewrite the URL.

Following Tomcat documentation for rewriting URLs, I created context.xml and rewrite.config in the WEB-INF directory of my webapp, but still no luck. Here are the files:

ROOT/WEB-INF/context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <Valve className="org.apache.catalina.valves.rewrite.RewriteValve" />
</Context>

ROOT/WEB-INF/rewrite.config

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Any help is much appreciated.


Solution

  • I ended up using Tuckey UrlRewriteFilter and removed ROOT/WEB-INF/context.xml and ROOT/WEB-INF/rewrite.config. Now the Tomcat is serving WordPress perfectly and everything is working fine.

    Here are the steps to setup UrlRewriteFilter (taken from official website) for WordPress on Tomcat, which maybe useful to somebody:

    Download UrlRewriteFilter.jar into ROOT/WEB-INF/lib/ and follow all the instructions/steps on its official website. If you want to use .htaccess syntax to define rewrite rules, then you can set a param modRewriteConf to true in ROOT/WEB-INF/web.xml as below:

    <!-- URLRewriteFilter -->
    <filter>
        <filter-name>UrlRewriteFilter</filter-name>
        <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
        <init-param>
            <param-name>modRewriteConf</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>UrlRewriteFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>
    

    Then create a .htaccess file in ROOT/WEB-INF/ directory (can be placed elsewhere but then you've to specify path via a param in web.xml).

    Note: I had to comment out a RewriteRule in order to make it work successfully.

    RewriteEngine On
    RewriteBase /
    # RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]