Forum Moderators: phranque

Message Too Old, No Replies

Trouble with RewriteRules

         

supermanjnk

5:18 pm on Oct 6, 2006 (gmt 0)

10+ Year Member



RewriteRule ^whatever/([^/]*)/$ file.php?sect=$1
RewriteRule ^whatever/([^/]*)/([^/]*)/$ file.php?sect=$1&cat=$2
RewriteRule ^whatever/([^/]*)/([^/]*)/([^/]*)/$ file.php?sect=$1&cat=$2&id=$3

This works with
mydomain.com/whatever/section/category/id/

I would like to get rid of the whatever so that I just have:
mydomain.com/section/category/id/
but everything i try ends up breaking

Any sugestions would be great.

jdMorgan

7:06 pm on Oct 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Without a better definition of what you mean by "keeps breaking," it's hard to give specific suggestions.

I guess you're having problems because the rule will rewrite anything ending in slash, including URLs you don't want rewritten.

Two ways to fix this are to prevent rewriting if the requested file actually exists, or to explicitly prevent certain subdirectories from being rewritten:


RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/$ /file.php?sect=$1 [L]
#
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/([^/]*)/$ /file.php?sect=$1&cat=$2 [L]
#
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/$ /file.php?sect=$1&cat=$2&id=$3 [L]


RewriteCond $1 !^images
RewriteCond $1 !^shared
RewriteRule ^([^/]*)/$ /file.php?sect=$1 [L]
#
RewriteCond $1 !^foo/images
RewriteCond $1 !^bar/scripts
RewriteCond $1 !^widgets/green
RewriteRule ^([^/]*)/([^/]*)/$ /file.php?sect=$1&cat=$2 [L]
#
RewriteCond $1 !^random/foo/images
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/$ /file.php?sect=$1&cat=$2&id=$3 [L]

Note that the checks are only needed on rules where the pattern may match something you don't want to rewrite.

Many more variations and alternate implementations are possible, depending on your directory and file structure, so the above stand only as examples.

Jim

[edited by: jdMorgan at 7:08 pm (utc) on Oct. 6, 2006]

supermanjnk

11:02 pm on Oct 6, 2006 (gmt 0)

10+ Year Member



Thanks a lot. That was exactly what I was looking for.