Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Weird behavior of mod_rewrite on existing directory

0 views
Skip to first unread message

CoolCow

unread,
Nov 4, 2009, 7:49:15 PM11/4/09
to
Hi,

I'm using mod_rewrite to rewrite all non-existing files/directory's to
my index.php. That's easy. But this time i also want it to perform a
rewrite on some existing directory's.

Here's what i have now:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_FILENAME} /(admin|pages)
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

As you see I'm also rewriting when they try to access the admin or
pages directory (2 directory's that actually exist)

When i open www.site.com/admin/test/blabla/ the query parameter q gets
set correctly to "/admin/test/blabla/".
However when i open www.site.com/admin (with no ending / ) apache
redirects me to "www.site.com/admin/?q=admin"

Why is

CoolCow

unread,
Nov 4, 2009, 8:23:14 PM11/4/09
to
On 5 nov, 01:49, CoolCow <cool....@gmail.com> wrote:
> Hi,
>
> I'm using mod_rewrite to rewrite all non-existing files/directory's to
> my index.php. That's easy. But this time i also want it to perform a
> rewrite on some existing directory's.
>
> Here's what i have now:
> RewriteCond %{REQUEST_FILENAME} !-f
> RewriteCond %{REQUEST_FILENAME} !-d [OR]
> RewriteCond %{REQUEST_FILENAME} /(admin|pages)
> RewriteCond %{REQUEST_URI} !=/favicon.ico
> RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
>
> As you see I'm also rewriting when they try to access the admin or
> pages directory (2 directory's that actually exist)
>
> When i openwww.site.com/admin/test/blabla/the query parameter q gets

> set correctly to "/admin/test/blabla/".
> However when i openwww.site.com/admin(with no ending / ) apache

> redirects me to "www.site.com/admin/?q=admin"
>
> Why is

Found the solution to this "weird behavior".

I just found out that the redirect is performed by apache itself, not
mod_rewrite.
Basically if you try to access a directory without a tailing / it adds
one by itself.

So what i did to solve my problem is right before rewriting to
index.php?q=....
just check if it's a directory and if it has a tailing /. If it
doens't add one myself.

Like this:
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} !^.*/$
RewriteRule ^(.*)$ $1/ [L,QSA]

0 new messages