Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need help with .htaccess rewriting...can't seem to grasp concept(s)...

Status
Not open for further replies.

spewn

Programmer
May 7, 2001
1,034
okay...

let's say i'm making a snack site. on this site there are different categories, with multiple subcategories.

donuts
-powdered
-cream
-sugar
-chocolate

cookies
-chocolate
-peanut butter
-oatmeal raisin

candy
-hard
-gum
-lollipops
-jawbreakers

what i'm trying to accomplish is to route depending on cat and sub cat...such as:

Code:
[URL unfurl="true"]http://snacksite.com/index.pl?a=category&b=subcategory&c=snackitem[/URL]

and the way i'm sending it is:

Code:
[URL unfurl="true"]http://snacksite.com/category/subcategory/snackitem[/URL]


i've gotten this far:

Code:
RewriteRule ^donuts /index.pl?a=donuts

which works, but that's as far as i've gotten.

any help/ideas?

thanks!

- g
 
okay...

i've modified your idea as follows:

Code:
RewriteRule ([^/]+)/([^/]+)/([^/]+)/(.+) /index.pl?a=$1&b=$2&c=$3&d=$4 [L]

and it works good.

i can have:

Code:
[URL unfurl="true"]http://snacksite.com/browse/category/subcategory/snackitem[/URL]

and it puts $a, $b, $c, $d perfect.

however, if i have:

Code:
[URL unfurl="true"]http://snacksite.com/browse/category/subcategory[/URL]

it doesn't work.

seems if i don't have 4 places, it throws a page not found error.

any ideas?

- g
 
i don't understand what that means.

actually, i learn by example.

but more importantly, i'm not sure why it's not working.

any help?

- g
 
The rewriterule will only work if the regular expression is satisfied. Since it has 4 "arguments", it will only work/matches with 4 arguments. Any less and it doesn't rewrite. Check your access log or rewritelog, it is actually trying to get the page (without a rewrite).

You need to check that it HAS exactly 4 arguments and with rewritecond BEFORE doing the rewrite. Or if it has less arguments, then you need to rewrite it or 404 it differently.
 
okay...

so i did this:

Code:
RewriteRule ([^/]+)/([^/]+)/([^/]+)/(.+) /index.pl?a=$1&b=$2&c=$3&d=$4 [L]
RewriteRule ([^/]+)/([^/]+)/(.+) /index.pl?a=$1&b=$2&c=$3 [L]

but it didn't quite work out.

sorry for being so novice, but i'm just learning this .htaccess manipulation.

how do i account for 1, 2, 3 or 4 arguments?

- g
 
Hi

spewn, the two directives you posted above works just fine for me. I would say the error is elsewhere this time.

Alternatively this also works for me. Try it :
Code:
RewriteRule ([^/]+)/([^/]+)/([^/]+)(?:/(.+))? /ttindex.php?a=$1&b=$2&c=$3&d=$4

Feherke.
 
Obviously, Feherke is way way better at regular expressions than I am. Works for me, put a [R] to make it a redirect (non-local).
 
okay...thanks so far.

seems this:

Code:
RewriteRule ([^/]+)/([^/]+)/([^/]+)/(.+) /index.pl?a=$1&b=$2&c=$3&d=$4 [L]
RewriteRule ([^/]+)/([^/]+)/(.+) /index.pl?a=$1&b=$2&c=$3 [L]

works for me. however, when my page trys to render images, this rule comes into play, and thus my images will not show. this also happens for my path to javascript files.

my images are set up like:

Code:
[URL unfurl="true"]http://snacksite.com/pics/snacks/1.gif[/URL]

and also:

Code:
[URL unfurl="true"]http://snacksite.com/pics/snacks/donuts/1a.gif[/URL]

arrrggghhh!!

i can't figure this one out, although i'm trying.

it's almost like i have to put "if .gif, .jpg, .bmp, .js or .css then ignore" rule but i don't know enough to make that work.

any help?

- g
 
Have it ignore /pics by using a rewritecond:

RewriteCond %{REQUEST_URI} !/pics/
RewriteRule ([^/]+)/([^/]+)/([^/]+)(?:/(.+))? /ttindex.php?a=$1&b=$2&c=$3&d=$4
 
ok...

thanks for the help!

i found this:

Code:
RewriteRule .+\.(js|css|gif|jpe?g|bmp)$ - [NC,L]

which seems to mean if file is .js, .css., .gif, .jpeg, .jpg or .bmp, don't apply rewrite rule.

seems to be working!

thanks again.

i'm gonna continue plugging away.

- g
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top