Forum Moderators: phranque

Message Too Old, No Replies

Using $ GET with mod-rewrite

$_GET vars not being set separately

         

kroleig1

10:25 pm on Sep 17, 2009 (gmt 0)

10+ Year Member



Hi,

Please forgive me if this question has been answered before. I have searched this and other forums for the past 2 days now and not found a simple solution to my problem.

I have mod-rewrite working. I have set up static linking for 2 levels deep in my site directory structure.

The problem is that I have added a 3rd level under my articles category of folders and added a rewrite rule to match. The redirect works fine, except I am unable to get the specific $1 and $2 values out when I use $_GET in my config script.

Here is my .htaccess code line:
# Three levels deep - for articles and its subcat folders..
RewriteRule ^/?articles/([a-zA-Z_^/]+)/([a-zA-Z_^/]+)$ /index.php?subj=articles&cat=$1&page=$2 [L]

So I am able to do a $_GET['subj'] and it gets the value 'articles', however, when I do $_GET['cat'] it says it doesnt exist! And if i do $_GET['page'] I get the value as 'cat.page' (it appends the cat value and the page value ignoring the / I used as a separator.

Can someone please help me understand why they are being concatenated like this?

I appreciate it

Thanks

jdMorgan

2:20 am on Sep 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> it appends the cat value and the page value ignoring the / I used as a separator.

Your script isn't going to 'see' slashes as separators, because the slashes don't get copied into the query string passed to your script. The "separator" is an ampersand ("&") not a slash ("/").

To be clear, in your rule, mod_rewrite takes the client-requested localized URL-path on the left, and rewrites it to the server filepath on the right, copying the slash-delimited 'parameters' from the URL-path "levels" into the named query-string-parameter values.

Your rewriterule pattern looks problematic, because it accepts slashes inside the "levels." It looks like you may be trying to make an exclusive character-group and an inclusive character-group at the same time... which isn't possible.

If your intent is to accept only letters, numbers, and underscores in each "level," then I'd suggest:


RewriteRule ^articles/([a-zA-Z_]+)/([a-zA-Z_]+)$ /index.php?subj=articles&cat=$1&page=$2 [L]

If your intent is to accept anything except a slash in each level, then I'd suggest:

RewriteRule ^articles/([^/]+)/([^/]+)$ /index.php?subj=articles&cat=$1&page=$2 [L]

[added] Do be sure to make the same correction to your two-parameter rule; If that rule precedes this three-parameter rule and uses the same faulty pattern, then this three-parameter rule will never run, and that would explain your problem. [/added]

If the meaning of character-group negation isn't clear, spend some time with the regular-expressions tutorial cited in our Apache Forum Charter.

If none of the above suggestions help, then I suspect that your script isn't parsing the query string properly.

Jim

kroleig1

3:05 am on Sep 18, 2009 (gmt 0)

10+ Year Member



Thanks JD

What you have said makes sense.. I will try it tonight and let you know if its been resolved.

Thanks

kroleig1

4:33 am on Sep 18, 2009 (gmt 0)

10+ Year Member



Jim,

Thanks so much Sir, you were correct. It works just like I expected. I won't be forgetting that anytime soon.

God bless
Roger