aspose file tools*
The moose likes Tomcat and the fly likes htaccess and mod_rewrite issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "htaccess and mod_rewrite issue" Watch "htaccess and mod_rewrite issue" New topic
Author

htaccess and mod_rewrite issue

Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 508
    
    6
those look complicated!

1. URLRewriteFilter can understand mod_rewrite rules (or so it claims - I haven't tried it out). You can check how to do that from their manual. I think an additional filter init-param is required for that.

2. I'll explain one rule by breaking it down. You'll have to work out the equivalent URLFilter rule using some trial and error:

^product-([^_]*)_([^_]*)\.jsp&pic=([^_]*)$ /showdetails.jsp?productID=$2&pic=$3

^product- ==> url starting with "product-"
([^_]*) ==> [^_] means any character except underscore
[^_]* means any character except underscore occurring 0 or many times.
([^_]*) the paranthesis means this group can be referred later on using $1 (because it's the 1st group in paranthesis. The next group can be referred using $2, $3, etc)

In a URL like
product-abc_123.jsp&pic=image

$1=abc, $2=123, $3=image

So the rewritten rule should become
/showdetails.jsp?productID=123&pic=image


Gary Sheldon
Ranch Hand

Joined: Nov 21, 2011
Posts: 44
Thank you for that demo, trial and error I can cope with, is there a way of turning on logging so that I can see what link is trying to be passed, that way I can amend syntax accordingly. If I just get a 404 error I won't stand a chance.
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 508
    
    6
Yes, the manual says how to turn on logging. You have to add an init-param called "logLevel" and set to "DEBUG". It'll output its log messages along with other Tomcat log messages in a log file with filename like "localhost-xxx" in tomcat logs directory.
Gary Sheldon
Ranch Hand

Joined: Nov 21, 2011
Posts: 44
Thank you the logging that is enabled now and working to a degree. The logs show what rules are being parsed, but if I click a link to that produces a 404 error the log doesn't show what is attempting to be parsed
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 508
    
    6
The logs show what rules are being parsed, but if I click a link to that produces a 404 error the log doesn't show what is attempting to be parsed

It logs every URL it received, and which rule it matched. If a match is found, it logs something like
"Rule 0 run called with <url>....matched "From"
If you don't see the "matched from" lines but only "rule 0 run called" kind of lines, then there's something wrong with your rules.
Gary Sheldon
Ranch Hand

Joined: Nov 21, 2011
Posts: 44
I've add the following parameters to my web.xml file and it appears to be using the .htacess file, but there seems to be an issue with the conf:

<init-param>
<param-name>modRewriteConf</param-name>
<param-value>true</param-value>
</init-param>

The log shows as follows:

28-Nov-2011 20:26:23 org.apache.catalina.core.ApplicationContext log
INFO: org.tuckey.web.filters.urlrewrite.UrlRewriteFilter INFO: loaded (conf ok)
28-Nov-2011 20:26:24 org.apache.catalina.core.ApplicationContext log
INFO: org.tuckey.web.filters.urlrewrite.UrlRewriteFilter INFO: loaded (conf ok)
28-Nov-2011 20:26:24 org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextInitialized()
28-Nov-2011 20:26:24 org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextInitialized()
28-Nov-2011 20:26:24 org.apache.catalina.core.ApplicationContext log
INFO: org.tuckey.web.filters.urlrewrite.utils.Log DEBUG: logLevel set to DEBUG
28-Nov-2011 20:26:24 org.apache.catalina.core.ApplicationContext log
INFO: org.tuckey.web.filters.urlrewrite.UrlRewriteFilter INFO: conf reload check performed each request
28-Nov-2011 20:26:24 org.apache.catalina.core.ApplicationContext log
INFO: org.tuckey.web.filters.urlrewrite.UrlRewriteFilter DEBUG: confPath set to /WEB-INF/.htaccess
28-Nov-2011 20:26:24 org.apache.catalina.core.ApplicationContext log
INFO: org.tuckey.web.filters.urlrewrite.utils.ModRewriteConfLoader DEBUG: processing line: RewriteEngine on
28-Nov-2011 20:26:24 org.apache.catalina.core.ApplicationContext log
INFO: org.tuckey.web.filters.urlrewrite.utils.ModRewriteConfLoader DEBUG: RewriteEngine value parsed as 'on'
28-Nov-2011 20:26:24 org.apache.catalina.core.ApplicationContext log
INFO: org.tuckey.web.filters.urlrewrite.utils.ModRewriteConfLoader DEBUG: RewriteEngine as boolean 'true'
28-Nov-2011 20:26:24 org.apache.catalina.core.ApplicationContext log
INFO: org.tuckey.web.filters.urlrewrite.utils.ModRewriteConfLoader DEBUG: processing line: RewriteRule Audi$ category.jsp?makeID=1
28-Nov-2011 20:26:24 org.apache.catalina.core.ApplicationContext log
INFO: org.tuckey.web.filters.urlrewrite.utils.ModRewriteConfLoader DEBUG: got rule Audi$ category.jsp?makeID=1
28-Nov-2011 20:26:24 org.apache.catalina.core.ApplicationContext log
INFO: org.tuckey.web.filters.urlrewrite.utils.ModRewriteConfLoader DEBUG: parsed rule part Audi$
28-Nov-2011 20:26:24 org.apache.catalina.core.ApplicationContext log
INFO: org.tuckey.web.filters.urlrewrite.utils.ModRewriteConfLoader DEBUG: parsed rule part category.jsp?makeID=1
28-Nov-2011 20:26:24 org.apache.catalina.core.ApplicationContext log
INFO: org.tuckey.web.filters.urlrewrite.UrlRewriteFilter DEBUG: inited with 1 rules
28-Nov-2011 20:26:24 org.apache.catalina.core.ApplicationContext log
INFO: org.tuckey.web.filters.urlrewrite.UrlRewriteFilter DEBUG: conf is NOT ok
28-Nov-2011 20:26:24 org.apache.catalina.core.ApplicationContext log
INFO: org.tuckey.web.filters.urlrewrite.UrlRewriteFilter ERROR: Conf failed to load

I've removed the complex rules, keeping it simple, would prefer to try and use the previous .htaccess files without re-writing at this stage, is there anything glaringly wrong here? The .htacess files is as follows:

RewriteEngine on
RewriteRule Audi$ category.jsp?makeID=1
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 508
    
    6
Looks ok to me - I'm unable to spot anything wrong with it. But obviously URLFilter doesn't like it for some reason. Asking on their discussion google group may throw some light.
Gary Sheldon
Ranch Hand

Joined: Nov 21, 2011
Posts: 44
Unfortunately, I'm no further forward, I've posted on the google group, but no joy.

I wonder if we could try and get the other rules working using the XML format and ignoring the htaccess route.

Would you mind helping me a little further?
Gary Sheldon
Ranch Hand

Joined: Nov 21, 2011
Posts: 44
Just thought, wondered if it may be the format of the .htaccess file, I believe there are issues regarding being ascii and also issues relating to white space/carriage returns, etc... Could you please create me a working .htacess file with the following:

RewriteEngine on
RewriteRule /Audi$ category.jsp?makeID=1

And another version with the following:

RewriteEngine on
RewriteRule /Audi$ category.jsp?makeID=1
RewriteRule /xindex.php /index.jsp
RewriteRule (.*)\.php $1.jsp
RewriteRule ^product-([^_]*)_([^_]*)\.jsp&pic=([^_]*)$ /showdetails.jsp?productID=$2&pic=$3 [L]
RewriteRule ^product-([^_]*)_([^_]*)\.jsp$ /showdetails.jsp?productID=$2 [L]
RewriteRule ^performance-tuning-([^_]*)_([^_]*)_([^_]*)\.jsp$ /performance_tuning.jsp?makeID=$2&modelID=$3 [L]
RewriteRule ^performance-tuning-([^_]*)_([^_]*)\.jsp$ /performance_tuning.jsp?makeID=$2 [L]
RewriteRule ^accessories-([^_]*)_([^_]*)_([^_]*)\.jsp$ /accessories.jsp?makeID=$2&modelID=$3 [L]
RewriteRule ^accessories-([^_]*)_([^_]*)\.jsp$ /accessories.jsp?makeID=$2 [L]
RewriteRule ^used-parts-([^_]*)_([^_]*)_([^_]*)\.jsp$ /used_parts.jsp?makeID=$2&modelID=$3 [L]
RewriteRule ^used-parts-([^_]*)_([^_]*)\.jsp$ /used_parts.jsp?makeID=$2 [L]
RewriteRule ^car-([^_]*)_([^_]*)_([^_]*)\.jsp$ /category.jsp?makeID=$2&modelID=$3 [L]

Are the [L] parameters an issue? If. so is there an equivelant for URLFilter.

Many thanks again :-)

Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 508
    
    6
I don't know what's the problem with the .htaccess, and don't know why URLRewriteFilter fails to load it either. I've never used that feature before. Since they mention it's beta quality, it's possible it has some problems.

You can revert back to using the urlrewrite.xml (by removing modRewriteConf init-param). Most of the rules should work even in XML, with only a few changes:

1. Insert the attribute use-query-string="true". This is required because I can see some rules involve parsing the query params too.


2. Translate the rules like this. If rule is
RewriteRule ^product-([^_]*)_([^_]*)\.jsp&pic=([^_]*)$ /showdetails.jsp?productID=$2&pic=$3 [L]

in XML it should become

<rule>
<from>/product-([^_]*)_([^_]*)\.jsp\?pic=([^_]*)$</from>
<to>/showdetails.jsp?productID=$2& amp;pic=$3</to>
</rule>


Note that
-the starting ^ is replaced by a /
-Any & should become "& amp;" in XML. [Important: There should be no space between & and amp; in your XML file. I'm forced to put a space here, because without it, the forum software double escapes it and shows only an &]

Also one of the rules
^product-([^_]*)_([^_]*)\.jsp&pic=([^_]*)$
has an error:
If pic is a query param, then it should be a ?, not an & like this. In the XML example I've given above, I'm assuming it's a ?
^product-([^_]*)_([^_]*)\.jsp?pic=([^_]*)$



Gary Sheldon
Ranch Hand

Joined: Nov 21, 2011
Posts: 44
Thank you, I've started adding the rule back in back in, but getting the following error "INFO: org.tuckey.web.filters.urlrewrite.ConfHandler DEBUG: error: The reference to entity "modelID" must end with the ';' delimiter." for the following code

<rule>
<from>/performance-tuning-([^_]*)_([^_]*)_([^_]*)\.jsp</from>
<to>/performance_tuning.jsp?makeID=$2&modelID=$3</to>
</rule>

<outbound-rule>
<from>/performance_tuning.jsp?makeID=$2&modelID=$3</from>
<to>/performance-tuning-([^_]*)_([^_]*)_([^_]*)\.jsp</to>
</outbound-rule>

If I modify the code to:

<rule>
<from>/performance-tuning-([^_]*)_([^_]*)_([^_]*)\.jsp</from>
<to>/performance_tuning.jsp?makeID=$2&modelID;=$3</to>
</rule>

<outbound-rule>
<from>/performance_tuning.jsp?makeID=$2&modelID;=$3</from>
<to>/performance-tuning-([^_]*)_([^_]*)_([^_]*)\.jsp</to>
</outbound-rule>

I get the following error:

INFO: org.tuckey.web.filters.urlrewrite.ConfHandler DEBUG: error: The entity "modelID" was referenced, but not declared.

Any idea? its like damned if I do and damned if I dont LOL
Gary Sheldon
Ranch Hand

Joined: Nov 21, 2011
Posts: 44
Oops, bit of a typo on the <to>/showdetails.jsp?productID=$2& amp;pic=$3</to> didn't notice the space between & and amp;pic removed the space and that error went away and then modified &modelID=$3 to be &modelID=$3 which made all the errors go away.

Do i need an outbound rule for every rule?

I'm gonna test things thoroughly, but things are looking up.

Take a bow, many thanks, you are a star
Gary Sheldon
Ranch Hand

Joined: Nov 21, 2011
Posts: 44
I'm back

Wonder if you could please cast some light on this if I had a URL called details.jsp?aID=1&bID=34&C=13 but wanted to the link to be product-blah_blah_blah_productname_1345.jsp how would I write that.

Cheers
Nguyen Phu Vinh
Greenhorn

Joined: May 23, 2014
Posts: 1
Gary Sheldon wrote:I'm back

Wonder if you could please cast some light on this if I had a URL called details.jsp?aID=1&bID=34&C=13 but wanted to the link to be product-blah_blah_blah_productname_1345.jsp how would I write that.

Cheers


I write english verry bad. but if you wanted to the link to be product-blah_blah_blah_productname_13413.jsp you change ([0-9]{1}[0-9]{2}[0-9]{2}) when browser will write *_13413.jsp

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: htaccess and mod_rewrite issue