Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite : specific conditions

         

dumbrava

12:30 pm on Apr 12, 2005 (gmt 0)

10+ Year Member



Hi all!

I have a rewrite wich looks like this:

a)

RewriteCond %{REQUEST_URI}!^/(([a-zA-Z]+)[.]([a-zA-Z]+))
RewriteRule ^/(.*) [localhost:6080...] [L,P]

It does rewrite from Apache which works on :80 to Zope which works as localhost:6080

That's OK.

b)

RewriteCond %{REQUEST_URI} ^/(([a-zA-Z]+)[.]([a-zA-Z]+))
RewriteRule ^/([a-zA-Z]+)[.]([a-zA-Z]+) /s.php?n=$1.$2

This one catch a pattern smth like: [SERVER...] and passes this (Firstname.Lastname) to my PHP script (/s.php?n=$1.$2
)

Problem is: In zope I have *.css files which are not being rewrited because of the /Firstname.Lastname pattern.

Does anybody have a solution for this?

Thanx in advance.

sitz

1:20 pm on Apr 12, 2005 (gmt 0)

10+ Year Member



Regarding b), you don't need the RewriteCond line; if the left-side of the RewriteRule doesn't match, the Rule won't be executed anyway. As to the CSS problem, it depends on how you want things to work. One possibly solution is this:

RewriteEngine on
RewriteRule ^/([^.]+)\.([^/]+)/?$ /s.php?n=$1.$2

This will only match if the path starts with $foo.$bar, there are no '.' characters in $foo, no '/' characters in $bar, and the URL ends after $bar or $bar/

This will handle situations like:

  • [example.com...]
  • [example.com...]

    But will not redirect, say, this:

  • [example.com...]
  • [example.com...]

    This may or may not be what you're going for; you didn't mention if you needed to be able to support having URL path info after the name.

    Does this help?

  • jd01

    2:01 pm on Apr 12, 2005 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    You should be able to add a condition to each of your previous sets, so you do not catch .css files, then add another that does:

    RewriteCond %{REQUEST_URI} ^!(.*)\.css
    RewriteCond %{REQUEST_URI} ^/(([a-zA-Z]+)[.]([a-zA-Z]+))
    RewriteRule ^/([a-zA-Z]+)[.]([a-zA-Z]+) /s.php?n=$1.$2 [L]

    RewriteCond %{REQUEST_URI} ^(.*).css
    RewriteRule ^(.*)\.css /path/to/$1.css [L]

    Been up all night, so please forgive me if the syntax is not perfect, but I think you will get the point.

    Remembered not to comment out this one...

    Hope this helps.

    Justin

    jd01

    2:02 pm on Apr 12, 2005 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Oops, missed the \

    dumbrava

    2:28 pm on Apr 12, 2005 (gmt 0)

    10+ Year Member



    Thank you guys for help!

    just 20 minutes ago I've used this solution and it works:

    RewriteCond %{REQUEST_URI}!^/(([a-zA-Z]+)[.]([a-zA-Z]+))
    RewriteRule ^/(.*) [localhost:6080...] [L,P]

    RewriteCond %{REQUEST_URI} ^/(([a-zA-Z]+)[.](css多tml夸pg夸peg在mp夙if))
    RewriteRule ^/(.*) [localhost:6080...] [L,P]
    RewriteCond %{REQUEST_URI}!^/(([a-zA-Z]+)[.]([a-zA-Z]+))
    RewriteRule ^/(.*) [localhost:6080...] [L,P]

    RewriteCond %{REQUEST_URI} ^/(([a-zA-Z]+)[.](css多tml夸pg夸peg在mp夙if))
    RewriteRule ^/(.*) [localhost:6080...] [L,P]

    #-------------------Catching / Vorname.Nachname---------

    RewriteCond %{REQUEST_URI} ^/(([a-zA-Z]+)[.]([a-zA-Z]+))
    RewriteCond %{REQUEST_URI}!^/(([a-zA-Z]+)[.](css多tml夸pg夸peg在mp夙if))
    RewriteRule ^/([a-zA-Z]+)[.]([a-zA-Z]+) /s.php?n=$1.$2

    In specially this line!^/(([a-zA-Z]+)[.](css多tml夸pg夸peg在mp夙if)) prevents not to catch.

    Sorry for my english.. :(

    jdMorgan

    2:44 pm on Apr 12, 2005 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    To make the code slightly smaller and faster, you can use the [NC] (No case) flag to make the compare case-insensitive. You can also eliminate several unnecessary characters. For example, replace:

    RewriteCond %{REQUEST_URI} !^/(([a-zA-Z]+)[.]([a-zA-Z]+))

    with:

    RewriteCond %{REQUEST_URI} !^/[a-z]+\.[a-z]+ [NC]

    Jim

    dumbrava

    2:50 pm on Apr 12, 2005 (gmt 0)

    10+ Year Member



    Thank you Jim,

    OK. Thank you all for your help!

    [edited by: dumbrava at 2:55 pm (utc) on April 12, 2005]