Forum Moderators: phranque

Message Too Old, No Replies

Help with MOD REWRITE with CONDITION

         

zeeshanhashmi

9:03 pm on Dec 27, 2008 (gmt 0)

10+ Year Member



Hello

I have a page that can have 3 variables with the url.
for example:

Code:

1) www.site.com/index.php?state=ST
2) www.site.com/index.php?case=BLAH
3) www.site.com/index.php?case=BLAH&state=ST
4) www.site.com/index.php?case=BLAH&town=SOMETOWN
5) www.site.com/index.php?case=BLAH&state=ST&town=SOMETOWN

I have generated the Rewrite Rules like this :

Code:

1) RewriteRule ^([^/]*)\.html$ /index.php?state=$1 [L]
2) RewriteRule ^([^/]*)\.html$ /index.php?case=$1 [L]
3) RewriteRule ^([^-]*)-([^-]*)\.html$ /index.php?case=$1&state=$2 [L]
4) RewriteRule ^([^-]*)-([^-]*)\.html$ /index.php?case=$1&town=$2 [L]
5) RewriteRule ^([^-]*)-([^-]*)-([^-]*)\.html$ /index.php?case=$1&state=$2&town=$3 [L]

I need Rewrite Condition for each of the case above.

Moreover, do i really need the condition ? can't I put all the 5 RewriteRules ? so that which ever is the suited one it will Rewrite Accordingly?

A billion thanks in advance

Zeeshan

jdMorgan

9:51 pm on Dec 27, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This will not work as you expect, because mod_rewrite has no way to tell which of the "virtual directory" values maps to which query string variable name. So, rule number 1 will always apply when one directory level is present in the requested URL, and rule number 2 will never execute. Similarly, rule #3 will always apply when two directory levels are present in the requested URL, and rule #4 will never execute.

You need to 'tag' the directory levels or URL-formats in some way, so that the URL carries some kind of information to tell mod_rewrite which of the query-string formats the URL should be rewritten to. Mod_rewrite looks only at the incoming requested URL defined by the clicked link on your page, and so the URL itself must tell mod_rewrite what to do.

This could be as simple as 'tagging' each virtual directory in the requested URL with a character-sequence, such as
/c-case/s-state.html -or-
/c-case/t-town.html

or you could combine the 'tags' in a top-level virtual directory, and use a URL like
/cs/case/state.html -or-
/ct/case/town.html

or perhaps reverse that, and use
/case/state/cs.html -or-
/case/town/ct.html

Once you decide how you want to do that, we can address some other coding problems with your rules.

Also, since you are rewriting these URLs, you might also want to consider simply leaving off the ".html" at the end, since it serves no real purpose.

Jim