Profile

unixronin: Galen the technomage, from Babylon 5: Crusade (Default)
Unixronin

December 2012

S M T W T F S
      1
2345678
9101112131415
16171819202122
23242526272829
3031     

Most Popular Tags

Expand Cut Tags

No cut tags
Friday, July 13th, 2007 08:05 pm

Let me keep this short:  I'm trying to do something fairly simple with mod_rewrite on Apache 1.3.x, and mod_rewrite isn't cooperating.  As a matter of fact, mod_rewrite isn't doing ANYTHING.

Consider the following sample directives:

RewriteEngine On

RewriteLog /tmp/apache-rewrites

RewriteLogLevel 9

RewriteCond %{REQUEST_URI} .*babcom.* [NC]

RewriteRule ^/(.+) http://www.caerllewys.net/$1 [R]

It should be apparent that this test rule should simply munge any URL containing the string 'babcom' and redirect it to caerllewys.net.  (I don't care at this point whether the resulting URL is valid, I just want it to do SOMETHING.)

So, we turn the rewrite engine on.  We tell mod_rewrite to log to /tmp/apache-rewrites at log level 9, and it obligingly opens the log file.

And that's the last thing it ever does.  mod_info says the module is loaded and has parsed its rules.  But it's not doing ANYTHING.  It never rewrites, and it never logs a thing.

Have any of you geekly types ever run into this behavior before?  Am I missing something stupidly, blindingly obvious?  Is there some secret, undocumented restriction against using both mod_rewrite and mod_alias?  Does Apache just hate me?

Update:

Problem is solved.  The Apache documentation never mentions that if you're running vhosts, mod_rewrite directives need to go in the vhost sections, not in the global section.

Tags:
Saturday, July 14th, 2007 12:26 am (UTC)
1. is mod_rewrite loaded?
2. is the logfile suitably permissioned?
3. if you drop the RewriteCond and just have the above in the relevant vhost, what happens?


The above also would NOT redirect this URL:
"http://www.babcom.com/" because it does not match the regex in RewriteRule. (The .+ fails it).
Saturday, July 14th, 2007 12:50 am (UTC)
See above — I just found the solution. I was putting the mod_rewrite directives in the global section, because the documentation never mentioned they needed to be in the individual vhost sections.

(I'm assuming I still only turn RewriteEngine On once, in the global section.)
Saturday, July 14th, 2007 05:14 am (UTC)
*foreheadsmack*
Saturday, July 14th, 2007 06:39 am (UTC)
"The Apache documentation never mentions that if you're running vhosts, mod_rewrite directives need to go in the vhost sections, not in the global section."

I haven't seen it specifically highlighted anywhere, but it is documented.


RewriteEngine Directive
(http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewriteengine)
"Note that, by default, rewrite configurations are not inherited. This means that you need to have a RewriteEngine on directive for each virtual host in which you wish to use it."
Saturday, July 14th, 2007 04:42 pm (UTC)
Evidently, I somehow managed to miss that.
Saturday, July 14th, 2007 04:31 pm (UTC)
mod_rewrite directives need to go in the vhost sections, not in the global section.

OOOOOOOOOOOOOOoooooooooooooohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh.

Dammit.

So that's the problem.

(grumble grumble)
Saturday, July 14th, 2007 05:29 pm (UTC)
Yup.

Spent 2 days trying to figure out WTF it wasn't working - worked in the examples I had.

Gave up said "*@&$(*@$@#*(#@ Apache" and came up with a workaround on the filesystem.

Asked our "Web Guy" who does Apache for his job description about it, he said "I dunno, man, looks good to me."
Saturday, July 14th, 2007 05:38 pm (UTC)
Next order of business is to take the one rule that currently looks like this:

RewriteCond %{REQUEST_URI} /BADUSER/.*\.mp3$ [NC]
RewriteRule .* - [L,F]

and make it block by MIME type instead of file extension. Theoretically I should be able to do it using %{HTTP:Content-Type}. Then I can block video/* and audio/* regardless of file extension.