Forum Moderators: phranque

Message Too Old, No Replies

rewrite rules not working outside of <Directory>?

works within <Directory>, doesn't work outside

         

alavigne

6:15 pm on Nov 27, 2007 (gmt 0)

10+ Year Member



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

example.homeip.net -> example.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 when they are contained within a <Directory></Directory> block.

Here's the relevant fragment of my httpd.conf file:

<Directory "/var/www/html">

Options Indexes FollowSymLinks

AllowOverride None

Order allow,deny
Allow from all

</Directory>

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

Note the four Rewrite entries. If they are moved to within the <Directory> block, the rules work. However, if they are outside of the directory block, they don't work. My understanding is that Rewrite rules should work just fine 'globally' in the httpd.conf file. Not only that, but with my 2.0.54 Apache in my old FC4 setup, rules specified globally in this manner worked just fine.

Does anyone know of some new behaviour in Apache 2.2? Or something else specific about Fedora 8? What has changed?

...Andrew

[edited by: jdMorgan at 8:03 pm (utc) on Nov. 27, 2007]
[edit reason] examplified per TOS. [/edit]

jdMorgan

8:01 pm on Nov 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RewriteRule
Context: server config, virtual host, directory, .htaccess

The documentation indicates that RewriteRule will work at the server config level, in <VirtualHost> and <Directory> containers, and in .htaccess files. So, the problem is most likely that you do not have some setting that mod_rewrite requires when you are 'outside' the <Directory> container.

You did not mention any error log info, but I would say the likely problem is that you did not preface the mod_rewrite code with Options +FollowSymLinks or Options +SymLinksIfOwnerMatch either of which are required by mod_rewrite as documented.

Jim

alavigne

2:52 am on Nov 28, 2007 (gmt 0)

10+ Year Member



Hi - thanks for the reply. Here's what I have in the global section of my httpd.conf:

Options Indexes +FollowSymLinks +SymLinksIfOwnerMatch
RewriteLog "/var/log/httpd/rewrite_star2.log"
RewriteLogLevel 9
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.homeip\.net [NC]
RewriteCond %{HTTP_HOST}!^$
RewriteRule ^(.*) http://example.net/$1 [L,R=permanent]

As you can see, I do have an Options line in the global space. The rewrite is not performed when the above is in place, in the global space in httpd.conf. I even tried 'All' to turn on all of the Options, and even that didn't work. Tried 'MultiViews', too.

However, if I move the rules into the <Directory> block, they work fine.

[edited by: tedster at 7:39 pm (utc) on Nov. 28, 2007]
[edit reason] switch to example.net [/edit]

alavigne

2:53 am on Nov 28, 2007 (gmt 0)

10+ Year Member



oh, and I've been checking the error_log. No errors to report, no special or anomalous messages about mod_rewrite. I have the log level set to 'debug'.

alavigne

4:34 am on Nov 28, 2007 (gmt 0)

10+ Year Member



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.