You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Jack Bates <ms...@freezone.co.uk> on 2009/11/24 21:49:33 UTC

[users@httpd] RewriteRule in neither "foo/.htaccess" nor ".htaccess" can match http://example.com/.../foo

I'm struggling a bit with this mod_rewrite rule,

RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule . %{REQUEST_FILENAME}.html

My goal is, when someone requests http://example.com/.../foo, to respond
with "foo.html", if it exists

I originally tried doing this with MultiViews, but MultiViews won't
respond with "foo.html" if "foo" exists, and I have a case where I want,

* http://example.com/.../foo to respond with "foo.html",
* http://example.com/.../foo/ to respond with "foo/index.html",
* http://example.com/.../foo/bar to respond with "foo/bar.html"

I originally posted about how to do this with MultiViews here,
http://thread.gmane.org/gmane.comp.apache.user/87707

Aside from patching mod_negotiation, I think my only option is to use
mod_rewrite?

This %{REQUEST_FILENAME}.html rule is inferior to MultiViews because it
doesn't consider the Accept: request header - but it's good enough for
my case

My first struggle was with DirectorySlash - in server context, this
%{REQUEST_FILENAME}.html rule works, even with mod_dir enabled - but
in .htaccess context, mod_dir redirects requests for
http://example.com/.../foo to http://example.com/.../foo/ before the
rule can respond with "foo.html" instead

I set "DirectorySlash Off" to get around this

My second and current struggle is that in one case, the directory "foo"
already contains a .htaccess with,

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php

This rule works, before and after I added the %{REQUEST_FILENAME}.html
rule and "DirectorySlash Off" to a .htaccess in the parent directory -
but the %{REQUEST_FILENAME}.html rule doesn't work in this case -
requests for http://example.com/.../foo don't respond with "foo.html" :
(

I found that if I comment all the "Rewrite" statements out of
"foo/.htaccess", then the %{REQUEST_FILENAME}.html rule works again - I
guess any "Rewrite" directive in a .htaccess disables rules in .htaccess
files in parent directories?

So I tried copying the %{REQUEST_FILENAME}.html rule to "foo/.htaccess",
but this still doesn't work - I think "RewriteRule ^$ ..." in
"foo/.htaccess" matches http://example.com/.../foo/, but doesn't match
http://example.com/.../foo

So it seems there's no way for a mod_rewrite rule in .htaccess context
to match http://example.com/.../foo - with "RewriteRule .
%{REQUEST_FILENAME}.html" in the parent directory, and "RewriteRule ^$
%{REQUEST_FILENAME}.html" in "foo/.htaccess", the rule in
"foo/.htaccess" disables the rule in the parent directory, without
possibly matching http://example.com/.../foo

Can we please have on or the other? : )

Either rules in "foo/.htaccess" can match http://example.com/.../foo or
rules in the parent directory can match http://example.com/.../foo -
currently it seems neither is true : (

I checked that this is still the case with trunk revision 883548

I used this httpd.conf,
http://www.sfu.ca/~jdbates/tmp/apache/200911240/httpd.conf

- and these .htaccess files,
http://www.sfu.ca/~jdbates/tmp/apache/200911240/www.tgz

I didn't have any more success by adding "RewriteOptions inherit"

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] RewriteRule in neither "foo/.htaccess" nor ".htaccess" can match http://example.com/.../foo

Posted by André Warnier <aw...@ice-sa.com>.
Jack Bates wrote:
> I'm struggling a bit with this mod_rewrite rule,
> 
I am a bit intimidated by your detailed explanation and the level of 
expertise required to fully understand your requirements, never mind 
finding a solution. But reading your post in diagonals gives me the 
feeling that maybe the sum of your requirements is just a bit too much 
to be covered by generic modules like mod_rewrite et al., which after 
all are generic tools and not specifically designed to cover all 
possible cases and countercases one could come up with.
It seems like what you want is something like
/foo to deliver /foo.html
except if /foo really exists and is a directory, then you would want 
/foo/index.html to be delivered
except if the directory /foo contains a .htaccess, in which case you 
want the .htaccess-contained rewrite rules to apply
except if /foo is a sub-directory of another directory already 
containing a .htaccess with rewrite rules in it, in which case these 
should apply too
I may be jesting a bit, but don't you think this is a bit much to ask ?

In a case like this, maybe it is the layout of the site that needs to be 
revisited, or an ad-hoc custom fixup module (mod_perl ?) may be the 
second choice.
Purely personal opinion of course.


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org