logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Apache View previous topic :: View next topic
Reply to topic   Topic: Cannot get apache to use mod_rewrite before mod_jk!!!
Author
alavigne



Joined: 24 Nov 2007
Posts: 9

PostPosted: Sat 24 Nov '07 22:48    Post subject: Cannot get apache to use mod_rewrite before mod_jk!!! Reply with quote

Hi, everyone.

I've recently upgraded to Fedora 8 (and with that comes Apache 2.2.6 and Tomcat 5.5).

I'm using mod_rewrite to redirect

alavigne.homeip.net -> alavigne.net

This was working fine in my old Fedora Core 4 setup, but on my Fedora 8 system I'm having a problem: the rewrites work only for URLs which don't map to my Tomcat context.

I guess the basic point is this: anything being shunted over to tomcat doesn't get evaluated by mod_rewrite beforehand. I've turned on maximum debugging in mod_rewrite, and I only see mod_rewrite examining URLs when they don't map my Tomcat context. When they do map to Tomcat, there is nothing at all in my mod_rewrite log.

Here's the relevant bits of my httpd.conf:

-------------------------------

<Directory "/var/www/html">

Options Indexes FollowSymLinks

AllowOverride None

Order allow,deny
Allow from all

RewriteEngine on
RewriteCond %{HTTP_HOST} ^alavigne\.homeip\.net [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*) http://alavigne.net/$1 [L,R=permanent]

</Directory>

RewriteLog "/var/log/httpd/rewrite5.log"
RewriteLogLevel 9

#
# mod_jk config
#

JkWorkersFile /etc/httpd/conf/workers.properties

# Where to put jk logs
JkLogFile /var/log/httpd/mod_jk

# Set the jk log level [debug/error/info]
JkLogLevel error

# Select the log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "

# JkOptions indicate to send SSL KEY SIZE,
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories

#JkRequestLogFormat set the request format
JkRequestLogFormat "%w %V %T"

# Send everything for context /newHomePage to worker named default (ajp13)
JkMount /newHomePage/* default

--------------------------------

So, if I specify "http://alavigne.homeip.net/blah", it gets correctly rewritten to "http://alavigne.net/blah".

However, if I specify "http://alavigne.homeip.net/newHomePage/blah", mod_rewrite doesn't seem to be invoked at all. no logs, nothing, even at log level 9. It just gets forwarded straight to Tomcat.

So...............

I'm having a devil of a time trying to get this to work! I've been perusing the 'net for many hours now and I did see something to the effect of there being a security hole that was fixed in mod_jk 1.2.23 (I'm using mod_jk 1.2.25), and that one should use a new value for the JkOptions Directive for mod_jk. Well, I've tried all of the various +ForwardURIxxx options, and none of them helps. (the example up in my config file just happens to be the last option I tried).

I've been careful to place my LoadModule for mod_jk before that of mod_rewrite (something I've seen mentioned many times by others)

I even downloaded, compiled, and tried out mod_jk 1.2.22 (because I wanted to see if this worked before the security fix of mod_jk 1.2.23). No luck.

I'm fervently hoping that some kind soul out there has a little hint that'll help me resolve this.

Thanks muchly,
...Andrew
Back to top
tdonovan
Moderator


Joined: 17 Dec 2005
Posts: 611
Location: Milford, MA, USA

PostPosted: Sun 25 Nov '07 16:44    Post subject: Reply with quote

Your rewrite conditions and rule seem like they would work OK outside of a <Directory> block.
You might try moving them outside of your <Directory "/var/www/html"> block in httpd.conf.

Looking quickly at the mod_jk 1.2.25 source code, it claims to bypass the Apache directory walk step for non-file requests on Apache 2.
If so, any rewrite rules which are inside a <Directory>, <Files>, or <Location> block may get bypassed.

I don't use mod_jk, so I haven't tried this myself - but perhaps it is worth a quick test.

FYI - the source for mod_jk.c is here. Scroll to the bottom to see that there is a "map_to_storage" hook when the MODULE_MAGIC_NUMBER_MAJOR is greater than 20010808 (i.e. greater than Apache 2.0.23). The comments for the jk_map_to_storage function in mod_jk.c say this:
Quote:
#if (MODULE_MAGIC_NUMBER_MAJOR > 20010808)
/* bypass the directory_walk and file_walk for non-file requests */
static int jk_map_to_storage(request_rec * r)
...


-tom-
Back to top
alavigne



Joined: 24 Nov 2007
Posts: 9

PostPosted: Sun 25 Nov '07 22:21    Post subject: Reply with quote

Hi, and thanks for the response.

Yeah, I've already tried putting my Rewrite conditions and rules outside of the <Directory> construct (in fact, outside in the global space is where I initially had them, and where I had them [working] in my httpd.conf on my older Fedora 4 system).

Perhaps I'll back and try moving them out again.

...Andrew
Back to top
alavigne



Joined: 24 Nov 2007
Posts: 9

PostPosted: Tue 27 Nov '07 1:54    Post subject: nope, global rewrite rules don't help Reply with quote

Well, I've reconfirmed - putting these same rules into the 'global' space in httpd.conf makes them not work, at all (at least within the Directory construct, the rewrite rules work so long as the URL doesn't map to the tomcat context).

I've read what you said about the mod_jk source... maybe I should try an older 2.0 version of apache, and see if things start to work again.

Any other ideas out there... anyone?

Thanks,
...Andrew
Back to top
alavigne



Joined: 24 Nov 2007
Posts: 9

PostPosted: Tue 27 Nov '07 5:31    Post subject: another update on this continuing saga Reply with quote

So, been working on this a bit more, and I'm starting to become less focused on mod_jk and more focused on the peculiar fact that my rewrite rules work in the <Directory> block, but don't work outside of it.

I do notice that the RewriteLog directive works within or outside of the directory block, because when I move the directives outside of the block, I do see a rewrite_log file being created with the log name that I specify, so it's seeing that, at least. But the rest of the directives... nope.

In summary, if I do this:

<Directory "/var/www/html">

Options Indexes FollowSymLinks

AllowOverride None

Order allow,deny
Allow from all

RewriteEngine on
RewriteCond %{HTTP_HOST} ^alavigne\.homeip\.net [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*) http://alavigne.net/$1 [L,R=permanent]

</Directory>

It works - the rewrite rules are honored, and I can do http://alavigne.homeip.net/blah.html and it gets rewritten to http://alavigne.net/blah.html

But if I remove the <Directory>, </Directory> tags, it doesn't work (of course I also have to remove AllowOverride, Order, and Allow, because they aren't allowed at the global level in the httpd.conf file).

Perhaps I should go back and try an older version of apache? The one I'm using is 2.2.6. The one I was using on my old server (where all of this worked perfectly fine) is 2.0.54.

...Andrew
Back to top
tdonovan
Moderator


Joined: 17 Dec 2005
Posts: 611
Location: Milford, MA, USA

PostPosted: Tue 27 Nov '07 15:03    Post subject: Reply with quote

I don't think you want to remove the whole <Directory> block with its Allow and Options directives, especially if it is your web root.

You should just move these four lines from inside the <Directory> block to outside it:
Quote:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^alavigne\.homeip\.net [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*) http://alavigne.net/$1 [L,R=permanent]

-tom-
Back to top
alavigne



Joined: 24 Nov 2007
Posts: 9

PostPosted: Tue 27 Nov '07 20:04    Post subject: Reply with quote

Hi, Tom

Yeah, Last night I happened to already try what you've just suggested. I left the <Directory> </Directory> block as it is, other than moving the RewriteEngine, RewriteCond, and Rewrite Rules out into the top-level of the httpd.conf file. So, I had this:

<Directory "/var/www/html">

Options Indexes FollowSymLinks

AllowOverride None

Order allow,deny
Allow from all

</Directory>

RewriteEngine on
RewriteCond %{HTTP_HOST} ^alavigne\.homeip\.net [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*) http://alavigne.net/$1 [L,R=permanent]


Again, it doesn't work when the rules are outside of <Directory>, and it does work when they are inside the block.
Back to top
tdonovan
Moderator


Joined: 17 Dec 2005
Posts: 611
Location: Milford, MA, USA

PostPosted: Tue 27 Nov '07 20:58    Post subject: Reply with quote

If you have this (outside the <Directory> block):
Quote:
RewriteLog logs/rewrite.log
RewriteLogLevel 9

it shows nothing at all in the log for the Tomcat requests?

That's odd!

Do you have a <Directory> block for the top level directory? - i.e. <Directory />

If so, what does it have for Options? Does it have FollowSymLinks?

-tom-
Back to top
alavigne



Joined: 24 Nov 2007
Posts: 9

PostPosted: Tue 27 Nov '07 21:24    Post subject: Reply with quote

tdonovan wrote:
If you have this (outside the <Directory> block):
Quote:
RewriteLog logs/rewrite.log
RewriteLogLevel 9

it shows nothing at all in the log for the Tomcat requests?

That's odd!

Do you have a <Directory> block for the top level directory? - i.e. <Directory />

If so, what does it have for Options? Does it have FollowSymLinks?

-tom-


yes - there's a <Directory /> block. Here it is.

<Directory />
Options Indexes FollowSymLinks
AllowOverride None
# Options FollowSymLinks JAL commented out
</Directory>


I'm not even bothering with mounting the tomcat context at the moment. I think if I figure out why the rewrite rules don't work at the global level, it'll fix my tomcat problem, too.
Back to top
alavigne



Joined: 24 Nov 2007
Posts: 9

PostPosted: Wed 28 Nov '07 6:36    Post subject: seems like it is the apache version!! Reply with quote

So... in my frustration, I decided to compile the version of apache I was using on my old system on my new system, to see if that was the difference. And voila, it was. I compiled and installed Apache 2.0.54 on my new system, used it instead of 2.2.6, and my rewrite rules started to work in the 'global' space in httpd.conf. As a result, my rewrite rules work within my tomcat context (which was the original problem which necessitated me having the rewrite rules at the global level).

So, I'm happy about that, but a bit concerned, still. Is there some sort of bug in Apache 2.2 that doesn't allow rewrite rules at the global level? I tried just about every combination of anything I could think of for a week, and couldn't get it to work. Within 5 minutes of going back to Apache 2.0.54, it works just fine.

I had a look at the differences between my 2.0.54 httpd.conf and my 2.2.6 httpd.conf, and there are several differences, but none that I would think would lead to a change in mod_rewrite behaviour. I have documented what I think are the principle differences between the two httpd.conf files, if anyone is interested.
Back to top
tdonovan
Moderator


Joined: 17 Dec 2005
Posts: 611
Location: Milford, MA, USA

PostPosted: Wed 28 Nov '07 16:21    Post subject: Reply with quote

re: "Is there some sort of bug in Apache 2.2 that doesn't allow rewrite rules at the global level?"

Not using Apache 2.2.6 with Windows or with CentOS5. I don't have Fedora 8.
Rewrite works fine at the server level.

With RewriteLogLevel 9 you should have something in your rewrite.log file - even it is only to pass the URI through unchanged, like this:
Quote:
127.0.0.1 - - [28/Nov/2007:09:17:32 --0500] [localhost/sid#7a1f90][rid#3430d00/initial] (2) init rewrite engine with requested uri /alpha/beta.html
127.0.0.1 - - [28/Nov/2007:09:17:32 --0500] [localhost/sid#7a1f90][rid#3430d00/initial] (3) applying pattern '^(.*)' to uri '/alpha/beta.html'
127.0.0.1 - - [28/Nov/2007:09:17:32 --0500] [localhost/sid#7a1f90][rid#3430d00/initial] (4) RewriteCond: input='localhost' pattern='^tom4' [NC] => not-matched
127.0.0.1 - - [28/Nov/2007:09:17:32 --0500] [localhost/sid#7a1f90][rid#3430d00/initial] (1) pass through /alpha/beta.html

If you don't have any entries like this, then RewriteEngine on is not working.

Is there any RewriteEngine off directive that may be overriding it? Redhat systems usually have several .conf files in /etc/httpd/conf.d (see the Wiki). Maybe there is something in one of these which is disabling the RewriteEngine.

Another good test is to enable mod_info and check the directives shown by requesting /server-info#mod_rewrite.c

-tom-
Back to top
alavigne



Joined: 24 Nov 2007
Posts: 9

PostPosted: Wed 28 Nov '07 16:42    Post subject: Reply with quote

tdonovan wrote:
re: "Is there some sort of bug in Apache 2.2 that doesn't allow rewrite rules at the global level?"

Not using Apache 2.2.6 with Windows or with CentOS5. I don't have Fedora 8.
Rewrite works fine at the server level.

With RewriteLogLevel 9 you should have something in your rewrite.log file - even it is only to pass the URI through unchanged, like this:
Quote:
127.0.0.1 - - [28/Nov/2007:09:17:32 --0500] [localhost/sid#7a1f90][rid#3430d00/initial] (2) init rewrite engine with requested uri /alpha/beta.html
127.0.0.1 - - [28/Nov/2007:09:17:32 --0500] [localhost/sid#7a1f90][rid#3430d00/initial] (3) applying pattern '^(.*)' to uri '/alpha/beta.html'
127.0.0.1 - - [28/Nov/2007:09:17:32 --0500] [localhost/sid#7a1f90][rid#3430d00/initial] (4) RewriteCond: input='localhost' pattern='^tom4' [NC] => not-matched
127.0.0.1 - - [28/Nov/2007:09:17:32 --0500] [localhost/sid#7a1f90][rid#3430d00/initial] (1) pass through /alpha/beta.html

If you don't have any entries like this, then RewriteEngine on is not working.

Is there any RewriteEngine off directive that may be overriding it? Redhat systems usually have several .conf files in /etc/httpd/conf.d (see the Wiki). Maybe there is something in one of these which is disabling the RewriteEngine.

Another good test is to enable mod_info and check the directives shown by requesting /server-info#mod_rewrite.c

-tom-



I agree there should be something in the rewrite_log - but there is not when I have the rules positioned globally. Not even one line.

Your other suggestion is a good one, and I went looking in the .conf files that Fedora 8 comes with. The only thing 'grep -i rewrite' returns is a couple of commented-out 'RewriteEngine On" and "RewriteOptions inherit" in a file called mod_cband.conf. So, in short, there are not any other RewriteEngine directives anywhere. Not in httpd.conf, not in any other files that httpd.conf includes.

I'll try your mod_info suggestion and see what it reveals.

Thanks,
...Andrew
Back to top


Reply to topic   Topic: Cannot get apache to use mod_rewrite before mod_jk!!! View previous topic :: View next topic
Post new topic   Forum Index -> Apache