You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Bill Moseley <mo...@hank.org> on 2006/08/11 02:01:20 UTC

[users@httpd] Help with mod_rewrite rules

I'm trying to use a skip rule after a RewriteCond, but then the
conditions are not checked.

I've got these rewrite rules:

        RewriteCond %{REQUEST_URI} \.(jpe?g|gif|png|txt|doc|ppt|pdf)$
        RewriteCond %{REQUEST_URI} !/css/
        RewriteRule ^/(.+)$ /domains/default/$1             [last]

The goal is for any for those file types to rewrite the path.  This
works fine and the log shows:

    init rewrite engine with requested uri /foo.txt
    applying pattern '^/(.+)$' to uri '/foo.txt'
    RewriteCond: input='/foo.txt' pattern='\.(jpe?g|gif|png|txt|doc|ppt|pdf)$' => matched
    RewriteCond: input='/foo.txt' pattern='!/css/' => matched
    rewrite /foo.txt -> /domains/default/foo.txt
    local path result: /domains/default/foo.txt



Now the problem.  When there's a path prefix of "Rural" then I want
to not rewrite to /domains/default but rather to /domains/Rural.

So this is my attempt:

        RewriteCond %{REQUEST_URI} \.(jpe?g|gif|png|txt|doc|ppt|pdf)$
        RewriteCond %{REQUEST_URI} !/css/

        RewriteRule ^/Rural - [skip=1,nocase]
        RewriteRule ^/(.+)$ /domains/default/$1             [last]
        RewriteRule ^/(?:Rural/)?(.+)$ /domains/Rural/$1    [last,nocase]

So, the idea is if it matches /Rural it skips a rule and uses the
last rule to rewrite to /domains/Rural.  Otherwise it rewrites to /domains/default.

But when I do this the RewriteCond no longer is matched for /foo.txt:

    init rewrite engine with requested uri /foo.txt
    applying pattern '^/Rural' to uri '/foo.txt'
    applying pattern '^/(.+)$' to uri '/foo.txt'
    rewrite /foo.txt -> /domains/default/foo.txt
    local path result: /domains/default/foo.txt
    prefixed with document_root to /home/moseley/WS2/root/domains/default/foo.txt
    go-ahead with /home/moseley/WS2/root/domains/default/foo.txt [OK]

but is for /Rural/foo.txt


    init rewrite engine with requested uri /Rural/foo.txt
    applying pattern '^/Rural' to uri '/Rural/foo.txt'
    RewriteCond: input='/Rural/foo.txt' pattern='\.(jpe?g|gif|png|txt|doc|ppt|pdf)$' => matched
    RewriteCond: input='/Rural/foo.txt' pattern='!/css/' => matched
    applying pattern '^/(?:Rural/)?(.+)$' to uri '/Rural/foo.txt'
    rewrite /Rural/foo.txt -> /domains/Rural/foo.txt
    local path result: /domains/Rural/foo.txt
    prefixed with document_root to /home/moseley/WS2/root/domains/Rural/foo.txt
    go-ahead with /home/moseley/WS2/root/domains/Rural/foo.txt [OK]

It's vaguely familiar that it's a problem with how the RewriteCond
fires after the RewriteRule.

Am I not remembering how skip works?  Can I not use RewriteCond this
way?



I suppose the simple way is the following:

        RewriteCond %{REQUEST_URI} \.(jpe?g|gif|png|txt|doc|ppt|pdf)$
        RewriteCond %{REQUEST_URI} !/css/
        RewriteCond %{REQUEST_URI} ^/Rural                  [nocase]
        RewriteRule ^/(?:Rural/)?(.+)$ /domains/Rural/$1    [last,nocase]

        # now non-rural, do the same.

        RewriteCond %{REQUEST_URI} \.(jpe?g|gif|png|txt|doc|ppt|pdf)$
        RewriteCond %{REQUEST_URI} !/css/
        RewriteRule ^/(.+)$ /domains/default/$1             [last]








-- 
Bill Moseley
moseley@hank.org


---------------------------------------------------------------------
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] Help with mod_rewrite rules

Posted by Joshua Slive <jo...@slive.ca>.
On 8/10/06, Joshua Slive <jo...@slive.ca> wrote:

> RewriteConds apply only to the RewriteRule immediately following them.
>  There are various ways to avoid having to write the same conds
> multiple times, for example:
> RewriteCond %{REQUEST_URI} !\.(jpe?g|gif|png|txt|doc|ppt|pdf)$

Should obviously be an [OR] on the end of there to properly reverse
the logic of the original.

> RewriteCond %{REQUEST_URI} ^/css/
> RewriteRule .* - [L]
>
> RewriteRule ^/(.+)$ /domains/default/$1             [last]
> RewriteRule ^/(?:Rural/)?(.+)$ /domains/Rural/$1    [last,nocase]
>
> Joshua.
>

---------------------------------------------------------------------
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] Help with mod_rewrite rules

Posted by Bill Moseley <mo...@hank.org>.
On Thu, Aug 10, 2006 at 08:38:32PM -0400, Joshua Slive wrote:
> On 8/10/06, Bill Moseley <mo...@hank.org> wrote:
> >I suppose the simple way is the following:
> >
> >        RewriteCond %{REQUEST_URI} \.(jpe?g|gif|png|txt|doc|ppt|pdf)$
> >        RewriteCond %{REQUEST_URI} !/css/
> >        RewriteCond %{REQUEST_URI} ^/Rural                  [nocase]
> >        RewriteRule ^/(?:Rural/)?(.+)$ /domains/Rural/$1    [last,nocase]
> >
> >        # now non-rural, do the same.
> >
> >        RewriteCond %{REQUEST_URI} \.(jpe?g|gif|png|txt|doc|ppt|pdf)$
> >        RewriteCond %{REQUEST_URI} !/css/
> >        RewriteRule ^/(.+)$ /domains/default/$1             [last]
> 
> RewriteConds apply only to the RewriteRule immediately following them.
> There are various ways to avoid having to write the same conds
> multiple times, for example:
> RewriteCond %{REQUEST_URI} !\.(jpe?g|gif|png|txt|doc|ppt|pdf)$
> RewriteCond %{REQUEST_URI} ^/css/
> RewriteRule .* - [L]
> 
> RewriteRule ^/(.+)$ /domains/default/$1             [last]
> RewriteRule ^/(?:Rural/)?(.+)$ /domains/Rural/$1    [last,nocase]

I see.  I was thinking the Skip feature wouldn't really count. ;)

I build the httpd.conf with templates so it's not much problem
repeating.

I also realized that only .html needs to get proxied to my backend,
so I can just look for any files with an extension that doesn't end
in .html:

        RewriteCond %{REQUEST_URI} !/css/
        RewriteCond %{REQUEST_URI} \.\w+$
        RewriteCond %{REQUEST_URI} !\.html$
        RewriteRule ^/(.+)$ /domains/default/$1             [last]

Thanks!

-- 
Bill Moseley
moseley@hank.org


---------------------------------------------------------------------
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] Help with mod_rewrite rules

Posted by Joshua Slive <jo...@slive.ca>.
On 8/10/06, Bill Moseley <mo...@hank.org> wrote:
> I suppose the simple way is the following:
>
>         RewriteCond %{REQUEST_URI} \.(jpe?g|gif|png|txt|doc|ppt|pdf)$
>         RewriteCond %{REQUEST_URI} !/css/
>         RewriteCond %{REQUEST_URI} ^/Rural                  [nocase]
>         RewriteRule ^/(?:Rural/)?(.+)$ /domains/Rural/$1    [last,nocase]
>
>         # now non-rural, do the same.
>
>         RewriteCond %{REQUEST_URI} \.(jpe?g|gif|png|txt|doc|ppt|pdf)$
>         RewriteCond %{REQUEST_URI} !/css/
>         RewriteRule ^/(.+)$ /domains/default/$1             [last]

RewriteConds apply only to the RewriteRule immediately following them.
 There are various ways to avoid having to write the same conds
multiple times, for example:
RewriteCond %{REQUEST_URI} !\.(jpe?g|gif|png|txt|doc|ppt|pdf)$
RewriteCond %{REQUEST_URI} ^/css/
RewriteRule .* - [L]

RewriteRule ^/(.+)$ /domains/default/$1             [last]
RewriteRule ^/(?:Rural/)?(.+)$ /domains/Rural/$1    [last,nocase]

Joshua.

---------------------------------------------------------------------
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