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

Gary Sheldon
Ranch Hand

Joined: Nov 21, 2011
Posts: 44
I have Apache 2.2.21 and Apache Tomcat 6.0.33 installed on Windows 7 64-bit Laptop (please don't anyone scream at me at this point I am new to this). I think I have things configured correctly, but I am having difficulty with a friends site. I have supposedly a copy of the entire site, but I am having an issue with the mod_rewrite aspect of the site.

I have installed mod_jk.so so that Apache is my frontend and all JSP requests are passed to be processed by Tomcat and I have the LoadModule rewrite_module modules/mod_rewrite.so uncommented and I believe is working as I cannot see any errors in the error logs other than if I click a link which is supposed to be redirected using mod_rewite and I end up with a page cannot be found error.

Can anyone tell me if this to be expected as part of a htaccess file which is located at the root of the site:

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

Also, do htaccess files natively work on a windows system, or is that my issue and does anyone know how I can rectify this problem?

Any help would be much appreciated :-)
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 496
    
    5
Welcome to the Ranch!
If I understood you correctly, you want
http://localhost/Audi == (internal rewrite) ==> http://localhost/category.jsp?makeID=1 == (mod_jk)==> http://localhost:8080/category.jsp?makeID=1
Is this correct?
Have you checked that Tomcat and mod_jk are working fine without mod_rewrite enabled? "http://localhost/category.jsp?makeID=1" should reach Tomcat with "JkMount /*.jsp"

If that's ok, then you can turn attention to rewrite module.
Now, .htaccess is mainly for directory specific rewrites. If you're not using different redirecting rules in each directory under htdocs, then you don't really require .htaccess, because it makes every file access less efficient. They do work in Windows by the way.
Instead of .htaccess, add these Rewrite directives in main httpd.conf file itself:
RewriteEngine on
RewriteRule /Audi$ /category.jsp?makeID=1 [PT]

The [PT] stands for passthrough. Without it, mod_jk never gets a chance to examine the URL because mod_rewrite is last in chain by default. With [PT], mod_rewrite rewrites the URL and then passes the new URL to other modules like mod_jk.
Gary Sheldon
Ranch Hand

Joined: Nov 21, 2011
Posts: 44
Thank you for taking the time to reply.

Yes you have understood 100% what I am trying to achieve. I can access the same page both via localhost and localhost:8080 so I think I'm right in assuming the mod_jk is working as it should. I have 3 other sites hosted in the tomcat webapps folder, so I'm trying to stay clear of the main httpd.conf file as much as I possibly can to refrain from affecting the other projects.

What does the line #oifrfgioeruhgireuhgiuhergi do?

Can I add the [PT] parameter to the htaccess file?

Again, thank you for taking time out to help
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 496
    
    5
What does the line #oifrfgioeruhgireuhgiuhergi do?

# lines are comment lines and are ignored by httpd. I have no idea what is "oifrfgioeruhgireuhgiuhergi" - perhaps a Welsh swear word

Can I add the [PT] parameter to the htaccess file?

Yes you can.
Gary Sheldon
Ranch Hand

Joined: Nov 21, 2011
Posts: 44
Phew, wondered if it may be some form of encrption key. Nice one re the welsh swear word LOL :-)

Ok, I opened the htaccess file in Notepad, added the [PT] parameter, saved the file and restarted both Tomcat and Apache. My htaccess file now reads the following:

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

Now, this is where it gets weird. If I view http://localhost/mysite1/category.jsp?makeID=1 I get the correct page, if I view http://localhost:8080/mysite1/category.jsp?makeID=1 I also get the correct page. If however I visit http://localhost:8080/mysite1/Audi I get a blank page but no error and if I visit http://localhost/mysite1/Audi I get a 404 page cannot be found error.

This is mental, head and brick wall come to mind.
Gary Sheldon
Ranch Hand

Joined: Nov 21, 2011
Posts: 44
Also, just stopped both tomcat and apache web servers, cleared logs and found these issues in the file C:\Apache\httpd\logs\error.log:

[Mon Nov 21 20:31:09 2011] [warn] Useless use of AllowOverride in line 31 of C:/Apache/tomcat/conf/auto/mod_jk.conf.
[Mon Nov 21 20:31:09 2011] [warn] Useless use of AllowOverride in line 36 of C:/Apache/tomcat/conf/auto/mod_jk.conf.
[Mon Nov 21 20:31:09 2011] [warn] Useless use of AllowOverride in line 87 of C:/Apache/tomcat/conf/auto/mod_jk.conf.
[Mon Nov 21 20:31:09 2011] [warn] Useless use of AllowOverride in line 92 of C:/Apache/tomcat/conf/auto/mod_jk.conf.
[Mon Nov 21 20:31:09 2011] [warn] Useless use of AllowOverride in line 125 of C:/Apache/tomcat/conf/auto/mod_jk.conf.
[Mon Nov 21 20:31:09 2011] [warn] Useless use of AllowOverride in line 130 of C:/Apache/tomcat/conf/auto/mod_jk.conf.
[Mon Nov 21 20:31:09 2011] [warn] Useless use of AllowOverride in line 174 of C:/Apache/tomcat/conf/auto/mod_jk.conf.
[Mon Nov 21 20:31:09 2011] [warn] Useless use of AllowOverride in line 179 of C:/Apache/tomcat/conf/auto/mod_jk.conf.
[Mon Nov 21 20:31:09 2011] [warn] Useless use of AllowOverride in line 218 of C:/Apache/tomcat/conf/auto/mod_jk.conf.
[Mon Nov 21 20:31:09 2011] [warn] Useless use of AllowOverride in line 223 of C:/Apache/tomcat/conf/auto/mod_jk.conf.
[Mon Nov 21 20:31:09 2011] [warn] Useless use of AllowOverride in line 256 of C:/Apache/tomcat/conf/auto/mod_jk.conf.
[Mon Nov 21 20:31:09 2011] [warn] Useless use of AllowOverride in line 261 of C:/Apache/tomcat/conf/auto/mod_jk.conf.
[Mon Nov 21 20:31:09 2011] [notice] Apache/2.2.21 (Win32) mod_jk/1.2.32 configured -- resuming normal operations
[Mon Nov 21 20:31:09 2011] [notice] Server built: Sep 9 2011 10:26:10
[Mon Nov 21 20:31:09 2011] [notice] Parent: Created child process 6908
[Mon Nov 21 20:31:09 2011] [warn] Useless use of AllowOverride in line 31 of C:/Apache/tomcat/conf/auto/mod_jk.conf.
[Mon Nov 21 20:31:09 2011] [warn] Useless use of AllowOverride in line 36 of C:/Apache/tomcat/conf/auto/mod_jk.conf.
[Mon Nov 21 20:31:09 2011] [warn] Useless use of AllowOverride in line 87 of C:/Apache/tomcat/conf/auto/mod_jk.conf.
[Mon Nov 21 20:31:09 2011] [warn] Useless use of AllowOverride in line 92 of C:/Apache/tomcat/conf/auto/mod_jk.conf.
[Mon Nov 21 20:31:09 2011] [warn] Useless use of AllowOverride in line 125 of C:/Apache/tomcat/conf/auto/mod_jk.conf.
[Mon Nov 21 20:31:09 2011] [warn] Useless use of AllowOverride in line 130 of C:/Apache/tomcat/conf/auto/mod_jk.conf.
[Mon Nov 21 20:31:09 2011] [warn] Useless use of AllowOverride in line 174 of C:/Apache/tomcat/conf/auto/mod_jk.conf.
[Mon Nov 21 20:31:09 2011] [warn] Useless use of AllowOverride in line 179 of C:/Apache/tomcat/conf/auto/mod_jk.conf.
[Mon Nov 21 20:31:09 2011] [warn] Useless use of AllowOverride in line 218 of C:/Apache/tomcat/conf/auto/mod_jk.conf.
[Mon Nov 21 20:31:09 2011] [warn] Useless use of AllowOverride in line 223 of C:/Apache/tomcat/conf/auto/mod_jk.conf.
[Mon Nov 21 20:31:09 2011] [warn] Useless use of AllowOverride in line 256 of C:/Apache/tomcat/conf/auto/mod_jk.conf.
[Mon Nov 21 20:31:09 2011] [warn] Useless use of AllowOverride in line 261 of C:/Apache/tomcat/conf/auto/mod_jk.conf.
[Mon Nov 21 20:31:09 2011] [warn] Useless use of AllowOverride in line 31 of C:/Apache/tomcat/conf/auto/mod_jk.conf.
[Mon Nov 21 20:31:09 2011] [warn] Useless use of AllowOverride in line 36 of C:/Apache/tomcat/conf/auto/mod_jk.conf.
[Mon Nov 21 20:31:09 2011] [warn] Useless use of AllowOverride in line 87 of C:/Apache/tomcat/conf/auto/mod_jk.conf.
[Mon Nov 21 20:31:09 2011] [warn] Useless use of AllowOverride in line 92 of C:/Apache/tomcat/conf/auto/mod_jk.conf.
[Mon Nov 21 20:31:09 2011] [warn] Useless use of AllowOverride in line 125 of C:/Apache/tomcat/conf/auto/mod_jk.conf.
[Mon Nov 21 20:31:09 2011] [warn] Useless use of AllowOverride in line 130 of C:/Apache/tomcat/conf/auto/mod_jk.conf.
[Mon Nov 21 20:31:09 2011] [warn] Useless use of AllowOverride in line 174 of C:/Apache/tomcat/conf/auto/mod_jk.conf.
[Mon Nov 21 20:31:09 2011] [warn] Useless use of AllowOverride in line 179 of C:/Apache/tomcat/conf/auto/mod_jk.conf.
[Mon Nov 21 20:31:09 2011] [warn] Useless use of AllowOverride in line 218 of C:/Apache/tomcat/conf/auto/mod_jk.conf.
[Mon Nov 21 20:31:09 2011] [warn] Useless use of AllowOverride in line 223 of C:/Apache/tomcat/conf/auto/mod_jk.conf.
[Mon Nov 21 20:31:09 2011] [warn] Useless use of AllowOverride in line 256 of C:/Apache/tomcat/conf/auto/mod_jk.conf.
[Mon Nov 21 20:31:09 2011] [warn] Useless use of AllowOverride in line 261 of C:/Apache/tomcat/conf/auto/mod_jk.conf.
[Mon Nov 21 20:31:09 2011] [notice] Child 6908: Child process is running
[Mon Nov 21 20:31:09 2011] [notice] Child 6908: Acquired the start mutex.
[Mon Nov 21 20:31:09 2011] [notice] Child 6908: Starting 64 worker threads.
[Mon Nov 21 20:31:09 2011] [notice] Child 6908: Starting thread to listen on port 80.

Do these mean anything, apologies for being a newbie :-)
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 496
    
    5
Gary Sheldon wrote:
RewriteEngine on
RewriteRule /Audi$ /category.jsp?makeID=1[PT]

Now, this is where it gets weird. If I view http://localhost/mysite1/category.jsp?makeID=1 I get the correct page, if I view http://localhost:8080/mysite1/category.jsp?makeID=1 I also get the correct page. If however I visit http://localhost:8080/mysite1/Audi I get a blank page but no error and if I visit http://localhost/mysite1/Audi I get a 404 page cannot be found error.


So the actual URL is "http://localhost/mysite1/Audi" and not "http://localhost/Audi"? That does make a difference. mod_rewrite rules are highly sensitive to relative URL as well as relative document path under /htdocs.
I assume "mysite1" is a subdirectory under <httpd_dir>\htdocs. If so, then move the .htaccess to <httpd_dir>\htdocs\mysite1 and modify it to:

Note that there is a space before the [PT]. It's important.

The "http://localhost:8080/mysite1/Audi" request goes directly to Tomcat, and is not affected in any way by all this rewrite and jk configurations. Why does it give a blank page and not a 404? I'm not sure, but I'm guessing there's a Tomcat exception that's available in tomcat logs if it's been logged. In any case, it's not related to all these other configurations.

Useless use of AllowOverride in line 31 of C:/Apache/tomcat/conf/auto/mod_jk.conf.

I'm not sure what this is.
Does your mod_jk.conf contain an "AllowOverride" directive? I don't think AllowOverride is a valid directive at all for mod_jk. It can be removed.
Also why is mod_jk.conf under "C:/Apache/tomcat/" and not "C:/Apache/httpd/conf"? has httpd been configured to load it from "C:/Apache/tomcat/conf/auto" (in which case I think it's ok though a little odd)?
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 496
    
    5
This is mental, head and brick wall come to mind.

Yes, I too found mod_rewrite syntax somewhat complex initially, mainly due to the regular expressions and handling of slashes in directories.
The best way to understand what's happening is to turn on the rewrite log by adding this to httpd.conf during development (remember to delete them on production server):

For each request, it gives the complete trace of mod_rewrite translations.

Gary Sheldon
Ranch Hand

Joined: Nov 21, 2011
Posts: 44
Hi again, apologies for not getting back sooner, but needed some sleep :-)

I have attached a few files which might help, the mod_jk file gets automatically created when the server reboots, so I have no control over that file. I've turned on the ReWrite Logs and when I started Apache I saw a file called rewrite appear in the logs folder, however when I click a link when is to redirect, nothins is being written into the log files.

What am I foing wrong?
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 496
    
    5
I think you forgot to attach the files. Don't see any here.
Gary Sheldon
Ranch Hand

Joined: Nov 21, 2011
Posts: 44
Won't allow me, do you have an email address I could send them to?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Please UseTheForumNotEmail. If you send the files to him, nobody else will be able to help you. Perhaps you can put them online somewhere else and then link to them here; posting their contents here will make your post too large.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Gary Sheldon
Ranch Hand

Joined: Nov 21, 2011
Posts: 44
I've tried uploading, but one file is a zip and the other is xml and unfortunatly neither are supported.

Apologies, wasn't meaning to breach this site's protocol.
Gary Sheldon
Ranch Hand

Joined: Nov 21, 2011
Posts: 44
Please try the following link for the files I wish to upload:

http://bitly.com/vEHOtl?r=bb
Gary Sheldon
Ranch Hand

Joined: Nov 21, 2011
Posts: 44
Fixed the Useless use of AllowOverride errors related to C:/Apache/tomcat/conf/auto/mod_jk.conf. The file as I said before is auto generated when Tomcat starts, but if i comment out all lines that begin with AllowOverride and save the file before I start Apache, then I don't get the error. Would love to know how to apply a central change so that when the file is auto generated I don't have to keep uncommenting out those promblematic lines.

Still have the issue with the mod_rewrite not working and I have found a new issue too:

INFO: validateJarFile(C:\Apache\tomcat\webapps\mysite1\WEB-INF\lib\servlet.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class

And I think that is causing an error on the localhost file:

SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.ClassNotFoundException: com.sun.media.jai.codec.FileSeekableStream
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
at ps.core.image.<init>(image.java:27)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:154)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:291)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:877)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:594)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1675)
at java.lang.Thread.run(Unknown Source)

Arrgghhh!!!

Any help on this would be much appreciated too :-)
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 496
    
    5
I went through your configuration files.
The main problem is that when Tomcat autogenerates that mod_jk.conf file, it's configuring it as a <Virtualhost>.
Now when you also configure rewrite rules in httpd.conf, they are not automatically inherited by a Virtualhost unless there's a "RewriteOptions Inherit" inside the Virtualhost section. But in your case it's autogenerated everytime Tomcat is started.
So editing the auto generated file is impractical.

Now to be honest, I never even knew tomcat can autogenerate mod_jk.conf till I saw this. I've always done it manually. It's not that tough either.
IMO, the Virtualhost generated by it just adds another moving part to your system and makes it unnecessarily more complex to manage.
The intention of auto generating is good I guess, but in this case it doesn't work out because you also have rewrite rules in the mix.

Another thing I notice is that "/mysite1" is an alias directory to a webapp directory. So that does change the picture quite a bit regarding htaccess.
Before suggesting anything further, I need some clarifications, because how to proceed depends on this:
- What is the reason you are using Apache in front of Tomcat? Is it for offloading static content like images to Apache? Or is it because you have multiple sites - some on httpd and only 1 on tomcat?
- Are you ok with getting rid of the tomcat auto generated configuration file?
- Are you familiar with programming java servlets?

Gary Sheldon
Ranch Hand

Joined: Nov 21, 2011
Posts: 44
With regards to getting rid, if you can let me know how, then I will be happy to do so.

As for programming Java Servlets, I'll give anything a go once LOL :-) I am confortable editing and compiling, but other than that I'm in your capable hands.
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 496
    
    5
Gary Sheldon wrote:With regards to getting rid, if you can let me know how, then I will be happy to do so.

That part is quite simple.

But before that, basically I'm trying to see if mod_rewrite can be rid altogether.
For that, I need to know the reason you are using Apache in front of Tomcat? Is it for offloading static content like images to Apache? Or is it because you have multiple sites - some on httpd and few on tomcat - and you want them all to be accessible on the same host & port?
And where do you get those make IDs from .."audi" => ID=1. Is it from a database?


Gary Sheldon
Ranch Hand

Joined: Nov 21, 2011
Posts: 44
As for Tomcat, purely for JSP reasons and mod_rewrite is required for simplifying urls for seo purposes and the id's are being passed to database queries, the database is mySQL.
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 496
    
    5
Ok.

It seems to me that httpd is actually unnecessary here. You can implement friendly URLs directly in Tomcat using servlets or using a servlet filter like the Tuckey URLRewriteFilter.
It gets rid of mod_rewrite, and if you don't mind serving static content from Tomcat, you can get rid of httpd entirely and make Tomcat listen on port 80 directly.
It also makes maintenance easier...right now, every new make of car requires you to add a new rule. By looking up in DB from a servlet, this becomes unnecessary.
But some knowledge of servlets and servlet filters is required to implement this approach.

Since you don't seem to be so comfortable with servlets, I'll just suggest the least effort way by keeping your existing system and changing only the configuration:

1. Restart Tomcat once, so that mod_jk.conf is regenerated with all defaults.

2. Now remove all the <Listener className="org.apache.jk.config.ApacheConfig"> lines in tomcat's server.xml to disable mod_jk.conf autogeneration,

3. Open mod_jk.conf.
Delete the <VirtualHost> and </VirtualHost> tag lines. This removes the VirtualHost from the mix, and takes all configurations to global level.
Delete the "ServerName localhost" line.
Delete the "Alias mysite1 ..." kind of lines. They interfere with .htaccess otherwise.


4. Add RewriteRules like this to your .htaccess in C:/Apache/httpd/htdocs:
RewriteRule mysite1/Audi$ /mysite1/category.jsp?id=1 [PT]
Note the missing slash before first "mysite1". Also note there's a space before "[PT]"
Whenever you need to add a rule, just add it to this .htaccess. No need to restart httpd when a rule is added, because it reads .htaccess on every request.

5. Edit: I notice in your httpd.conf that AllowOverride is none for /htdocs. Change it to All, so that .htaccesss can work.

6. Restart httpd and try the URL "http://localhost/mysite1/Audi". Should work.

7. If you see any 403 access denied kind of errors( example, for image resources in "/webapps/mysite1/images") in the browser developer console, add these 2 lines
Order allow,deny
Allow from all
to the relevant <Directory> sections in mod_jk.conf and restart apache.

Gary Sheldon
Ranch Hand

Joined: Nov 21, 2011
Posts: 44
Thank you so much, gives me something to have a go at. My system is backup at the mo so will action first thing.

Thank you again :-)
Gary Sheldon
Ranch Hand

Joined: Nov 21, 2011
Posts: 44
Apologies, but I tried all of that, but still no joy.

Might be better if I start from the beginning, maybe with a clean install. What would you recommend, i need JSP and a URL Rewrite of some kind, I don't mind if i just have Tomcat aslong as the WEB-INF files work. As long as I can reference the site as http://localhost/mysite1/Audi and in the background the server passes http://localhost/mysite1/category.jsp?id=1 behind the scenes and still be able to host http://localhost/mysite2 I don't mind.

It would of been nice if the original developer had provided details of the preferred hosting environment, so flying blind at the mo.

Again, thank you for your time :-)
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 496
    
    5
Apologies, but I tried all of that, but still no joy.

That's odd, because I've a httpd tomcat configuration working here (it too is on Windows) with exactly the configuration I described. Do you see any errors in logs?

i need JSP and a URL Rewrite of some kind, I don't mind if i just have Tomcat

If you're ready to try it out, then start with URLRewriteFilter. It's basically mod_rewrite for Tomcat.
You'll be writing the exact same rules as you already have, except that they'll have slightly modified syntax and will be in XML format. Their installation section, skeleton XML file and examples should get you started quickly.
If it works, then Tomcat requests like http://localhost:8080/mysite1/Audi should forward you to the JSP.

Next phase, stop httpd, and make tomcat listen on port 80 by changing the connector port in server.xml to 80. URLs like http://localhost/mysite1/Audi should now forward to JSP.

If those works out fine, then you can further simplify the rewriting by having just one rule that translates "http://localhost/mysite1/[car]" to "http://localhost/mysite1/category.jsp?make=car", and changing the JSP to look up details of car in database by name instead of ID.

All this is ok for localhost, but since your purpose is SEO friendly URLs, I suppose there's a site hosted on some production server. You should definitely double check that all this is feasible there first. What is feasible in a windows development machine may not be feasible in your hosting server (for example, changing the tomcat port).
Gary Sheldon
Ranch Hand

Joined: Nov 21, 2011
Posts: 44
Thanks, I'll have a look.

I have uninstalled Apache 2.2, uninstalled Tomcat 6 and re-installed Tomcat 6 and configured to port 80. Used the host manager to copy back the previous projects, so I'm back to a clean install so hoperfully your next steps will work.

I'm at work at the moment so will revisit this evening.

Thanks again :-)
Gary Sheldon
Ranch Hand

Joined: Nov 21, 2011
Posts: 44
Ok, I have done a clean Tomcat install, used the host manager to copy the neccessary sites back. I have downloaded http://urlrewritefilter.googlecode.com/files/urlrewritefilter-3.2.0.zip, extracted the files, copied urlrewrite-3.2.0 to the folder of C:\Apache\tomcat\webapps\mysite1\WEB-INF\lib and modified the C:\Apache\tomcat\webapps\mysite1\WEB-INF\web.xml to be as follows:

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>

<rule>
<from>/mysite1/Audi/</from>
<to>/mysite1/category.jsp?makeID=1</to>
</rule>
<outbound-rule>
<from>/mysite1/category.jsp?makeID=1</from>
<to>/mysite1/Audi/</to>
</outbound-rule>

<web-app>
<session-config>
<session-timeout>120</session-timeout>
</session-config>
</web-app>

I now get the following error in my catalina.2011-11-24 log file and mysite1 no longer works:

24-Nov-2011 21:09:02 org.apache.tomcat.util.digester.Digester fatalError
SEVERE: Parse Fatal Error at line 11 column 2: The markup in the document following the root element must be well-formed.
org.xml.sax.SAXParseException: The markup in the document following the root element must be well-formed.

I'm clearly doing something wrong.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Your XML file does not have one single root element. You should move the filter, filter-mapping, rule and outbound-rule elements inside the web-app element.
Gary Sheldon
Ranch Hand

Joined: Nov 21, 2011
Posts: 44
Ok, my xml now reads as follows, site is backup, behaves stlightly weird in the fact that graphics don't appear initially, leave a page click back to re-visit the page then all graphics are present:

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>

<rule>
<from>/mysite1/Audi/</from>
<to>/mysite1/category.jsp?makeID=1</to>
</rule>
<outbound-rule>
<from>/mysite1/category.jsp?makeID=1</from>
<to>/mysite1/Audi/</to>
</outbound-rule>
<session-config>
<session-timeout>120</session-timeout>
</session-config>
</web-app>

Redirection still doesn't work, what am I missing.

I appreciate your patience :-)
Gary Sheldon
Ranch Hand

Joined: Nov 21, 2011
Posts: 44
Still getting these errors too:

INFO: validateJarFile(C:\Apache\tomcat\webapps\mysite1\WEB-INF\lib\servlet.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class

And:

SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.ClassNotFoundException: com.sun.media.jai.codec.FileSeekableStream

Tomcat doesn't appear to live the servlet.jar file located in \WEB-INF\lib

Anything I need to do, so the site utilises this file correctly.
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 496
    
    5
Gary Sheldon wrote:Still getting these errors too:

INFO: validateJarFile(C:\Apache\tomcat\webapps\mysite1\WEB-INF\lib\servlet.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class


This is not an error, just an Info message. This message would be present in earlier logs too when site was working.
Certain jars like servlet API are already provided by installed Tomcat according to the specification versions it implements, and these "system jars" are not supposed to be included in application's WEB-INF/lib.
You can delete that servlet.jar from your WEB-INF\lib.
If your developer has included servlet jar in \lib, I wouldn't be very surprised if there are other unnecessary jars in there too. List all the jars in that directory here, and we can check which ones to remove. Keeping them there can lead to errors due to classloader hierarchies.


SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.ClassNotFoundException: com.sun.media.jai.codec.FileSeekableStream

It means JAI related jar file(s) are missing from \WEB-INF\lib. Google "JAI in tomcat" to find which JAI jars to put there.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61002
    
  65

You should never had servlet.jar (or jsp.jar) in WEB-INF/lib. You're lucky that it's not causing any problems.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Gary Sheldon
Ranch Hand

Joined: Nov 21, 2011
Posts: 44
Removed servlet.jar files, restarted tomcat and got no errors as fas as I could see, but as soon as I invoked a jsp page on the server I checked the logs and now showing the following error:

25-Nov-2011 08:59:03 org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextInitialized()
25-Nov-2011 08:59:03 org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextInitialized()
25-Nov-2011 08:59:03 org.apache.catalina.core.ApplicationContext log
INFO: org.tuckey.web.filters.urlrewrite.UrlRewriteFilter INFO: loaded (conf ok)
25-Nov-2011 08:59:18 org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.ClassNotFoundException: com.sun.media.jai.codec.FileSeekableStream
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
at ps.core.image.<init>(image.java:27)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:154)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:176)
at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)
at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)
at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:381)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:291)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:877)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:594)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1675)
at java.lang.Thread.run(Unknown Source)

Does this help at all, it looks like the UrlRewriteFilter is happier now, still not working, but I have a feeling that te above errors need addressing before things will work as they should. Apologies again for this, I have been given developer files but no config/hosting environment details, so flying blind, sorry.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

You're still missing the JAI library in your class path.
Gary Sheldon
Ranch Hand

Joined: Nov 21, 2011
Posts: 44
I tried installing jai-1_1_3-lib-windows-i586-jre and jai-1_1_3-lib-windows-i586 (CLASSPATH) but still no joy.

At this point I'm wondering whether my Windows 7 64bit with Java SE Development Kit 64bit may be an issue, should I uninstall all Java & Tomcat and install 32-bit versions?
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 496
    
    5
JAI documentation (http://download.java.net/media/jai/builds/release/1_1_3/INSTALL.html#disableMediaLib) says

Running Without Native Acceleration

JAI can be run without its native acceleration layer without loss of functionality. This may be accomplished either by using the Java Platform-Independent Version or by setting the system property com.sun.media.jai.disableMediaLib to true. Note that when running without the native layer unless the aforementioned property is set to true warning messages will be emitted to indicate the inability to load the native wrapper or libraries.


You can try downloading that platform independent version and putting all jars inside that zip file into \WEB-INF\lib. I don't know how, or for what purpose, the JSP is using JAI , but if it's not using anything fancy, hopefully just the JARs should be enough. Any idea what the JSP is using JAI for?
Gary Sheldon
Ranch Hand

Joined: Nov 21, 2011
Posts: 44
Ok, I've uninstalled Tomcat 6 64-bit, Java 6 SE Development Kit 64-bit, rebooted, cleaned registry. Re-installed Java SE Development Kit 32bit, jai-1_1_3-lib-windows-i586-jdk and Tomcat 6 32-bit. Reloaded all websites and stil getting the same error relating to missing jai libraries.

In the mysite1\WEB-INF\lib\ folder are the following files:

activation.jar
commons-fileupload.jar
core-renderer.jar
core-renderer-minimal.jar
FCKeditor-2.3.jar
itext-paulo-155.jar
mail.jar
minium.jar
mlibwrapper_jai.jar
mysql-connector-java-3.0.9-stable-bin.jar

Laptop will be existing a near by window soon!
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 496
    
    5
Gary Sheldon wrote:Ok, I've uninstalled Tomcat 6 64-bit, Java 6 SE Development Kit 64-bit, rebooted, cleaned registry. Re-installed Java SE Development Kit 32bit, jai-1_1_3-lib-windows-i586-jdk and Tomcat 6 32-bit. Reloaded all websites and stil getting the same error relating to missing jai libraries.


I think that was unnecessary. You could have first tried putting the JAI jars from platform independent download into \WEB-INF\lib first. Try it now on the new installation.


Laptop will be existing a near by window soon!

That's a bit radical . I think your JAI installation is proper - it probably isn't working because Tomcat is possibly pointing to the JRE, while you've installed JAI inside JDK. Changing Tomcat's configuration will simply complicate this discussion thread. So I'd try the simple thing first - download JAI platform independent version (it's there in the documentation) and copy the jars in that file to \WEB-INF\lib, restart Tomcat, and see if the JSP works.
Gary Sheldon
Ranch Hand

Joined: Nov 21, 2011
Posts: 44
You are a genius, I copied the files jai_codec.jar and jai_core to the folder C:\Program Files (x86)\Apache Software Foundation\Tomcat 6.0\lib and all errors weent away, nice one :-)

Okay, now we are somewhat off the beaten track, no thanks to me LOL back to gettin the url rewrite to work :-)
Gary Sheldon
Ranch Hand

Joined: Nov 21, 2011
Posts: 44
Hope you had a good weekend, I can confirm the installation of successful installation of UrlRewriteFilter:

28-Nov-2011 09:30:53 org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextInitialized()
28-Nov-2011 09:30:53 org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextInitialized()
28-Nov-2011 09:30:53 org.apache.catalina.core.ApplicationContext log
INFO: org.tuckey.web.filters.urlrewrite.UrlRewriteFilter INFO: loaded (conf ok)

I have the following default urlrewrite.xml file located in the directory /webapps/mysite1/WEB-INF:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.2//EN"
"http://tuckey.org/res/dtds/urlrewrite3.2.dtd">

<!--

Configuration file for UrlRewriteFilter
http://tuckey.org/urlrewrite/

-->
<urlrewrite>

<rule>
<note>
The rule means that requests to /test/status/ will be redirected to /rewrite-status
the url will be rewritten.
</note>
<from>/test/status/</from>
<to type="redirect">%{context-path}/rewrite-status</to>
</rule>


<outbound-rule>
<note>
The outbound-rule specifies that when response.encodeURL is called (if you are using JSTL c:url)
the url /rewrite-status will be rewritten to /test/status/.

The above rule and this outbound-rule means that end users should never see the
url /rewrite-status only /test/status/ both in thier location bar and in hyperlinks
in your pages.
</note>
<from>/rewrite-status</from>
<to>/test/status/</to>
</outbound-rule>


<!--

INSTALLATION

in your web.xml add...

<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
<init-param>
<param-name>logLevel</param-name>
<param-value>WARN</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

EXAMPLES

Redirect one url
<rule>
<from>/some/old/page.html</from>
<to type="redirect">/very/new/page.html</to>
</rule>

Redirect a directory
<rule>
<from>/some/olddir/(.*)</from>
<to type="redirect">/very/newdir/$1</to>
</rule>

Clean a url
<rule>
<from>/products/([0-9]+)</from>
<to>/products/index.jsp?product_id=$1</to>
</rule>
eg, /products/1234 will be passed on to /products/index.jsp?product_id=1234 without the user noticing.

Browser detection
<rule>
<condition name="user-agent">Mozilla/[1-4]</condition>
<from>/some/page.html</from>
<to>/some/page-for-old-browsers.html</to>
</rule>
eg, will pass the request for /some/page.html on to /some/page-for-old-browsers.html only for older
browsers whose user agent srtings match Mozilla/1, Mozilla/2, Mozilla/3 or Mozilla/4.

Centralised browser detection
<rule>
<condition name="user-agent">Mozilla/[1-4]</condition>
<set type="request" name="browser">moz</set>
</rule>
eg, all requests will be checked against the condition and if matched
request.setAttribute("browser", "moz") will be called.

-->

</urlrewrite>

I have the amended web.xml file located in the directory /webapps/mysite1/WEB-INF:

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<rule>
<from>/mysite1/Audi/</from>
<to>/mysite1/category.jsp?makeID=1</to>
</rule>
<outbound-rule>
<from>/mysite1/category.jsp?makeID=1</from>
<to>/mysite1/Audi/</to>
</outbound-rule>

<session-config>
<session-timeout>120</session-timeout>
</session-config>
</web-app>

Unfortunatly I am still getting the following error:

HTTP Status 404 - /mysite1/Audi/

I've checked the logs and I get no errors or warnings, any thoughts?
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 496
    
    5
It seems like you've added the <rule>s to your web.xml. They should go into urlrewrite.xml, not web.xml. Please read the filter's manual carefully.

Also, the paths in <from> elements are relative to your context directory, as the manual says.
So the rules should be without "/mysite1", like

Gary Sheldon
Ranch Hand

Joined: Nov 21, 2011
Posts: 44
GENIUS, what a start, it works, thank you so much

Can I ask one final thing, this is the rest of the .htaccess file, I removed all the other categories as they were the same format and all now work, but could do with a little guideance on these entries below:

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]

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