Forum Moderators: phranque

Message Too Old, No Replies

Problem with mod rewrite expressions

this is working for me, but i know it's sloppy...

         

David Bruning

8:50 pm on Apr 11, 2005 (gmt 0)

10+ Year Member



RewriteCond %{REQUEST_URI}!index\.php$
RewriteRule ^(.*)$ index.php?year=$1
RewriteRule ^(.*)\(.*)\$ index.php?year=$1&team=$2
RewriteRule ^(.*)\(.*)\(.*)$ index.php?year=$1&team=$2&game=$3 [L]

is what I am using.
I see 2 problems, 1) is resources, I have been told * is a hog. And 2) it seems that the middle rule overpowers the end rule when they both match. Just not sure how to resolve this.

Any suggestions would be awesome :)

jdMorgan

9:58 pm on Apr 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd suggest:

RewriteCond %{REQUEST_URI} !/index\.php$
RewriteRule ^([^/]+)/([^/]+)/(.+)/?$ index.php?year=$1&team=$2&game=$3 [L]
#
RewriteCond %{REQUEST_URI} !/index\.php$
RewriteRule ^([^/]+)/(.+)/?$ index.php?year=$1&team=$2 [L]
#
RewriteCond %{REQUEST_URI} !/index\.php$
RewriteRule ^(.+)/?$ index.php?year=$1 [L]

Your original rules shouldn't have worked. You had "\" in several places where "/" was needed. The reason that your original first rule overpowered the others is that "(.*)" should have matched any URL, giving "index.php?year=parm1/parm2/parm3 if a three-parameter URL was requested, and causing similar problems for two-parameter requests.

The rules were also reversed in order.

If index.php is a single file in the Web root directory, then you can make the rules even more efficient by using:


RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule ^([^/]+)/([^/]+)/(.+)/?$ /index.php?year=$1&team=$2&game=$3 [L]
#
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule ^([^/]+)/(.+)/?$ /index.php?year=$1&team=$2 [L]
#
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule ^(.+)/?$ /index.php?year=$1 [L]

(All RewriteConde patterns anchored and destination index.php file 'rooted')

I assume this code goes into .htaccess. If not, it will need some adjustements to run in httpd.conf.

Note that a RewriteCond only affects the single RewriteRule that immediately follows it.

Use [L] on all rules unless you know a reason not to.

Never use ".*" or ".+" in a pattern when it can be avoided. They are "easy," but very inefficient when used as the initial subpatterns in patterns with multiple subpatterns. I.e. It's OK to use them at the end of a pattern, but not at the beginning or in the middle.

Just for reference "[^/]+" means, "Match one or more characters except for slash," or more practically in this case, "Match all characters up to the next slash."

The "/?" at the end of each pattern will allow, but not require, a trailing slash.

See the documentation cited in our forum charter for more info.

Jim

David Bruning

10:05 pm on Apr 11, 2005 (gmt 0)

10+ Year Member



Thanks Jim :)

I am going to go over everything you posted with a fine tooth comb, I love mod rewrite and need to get my mind wrapped around it :)

jd01

3:48 am on Apr 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just something I have to do...

I have to use [QSA,L] when working with php to have variables passed correctly. QSA passes as a query string...

Maybe jdMorgan can shed some light on why, and if this is globally true?