Forum Moderators: phranque

Message Too Old, No Replies

.htaccess RewriteRule woes

problems with rewrite rules

         

MrMeAndMyself

10:37 pm on Jul 30, 2008 (gmt 0)

10+ Year Member



Hi,

I'm trying to wrap my head around url rewriting with htaccess, but at the moment it's like trying to wrap a very small piece of string round a truck.

I need to include 3 rewrite rules, all with the same operator (eh! fake directory), something like this.

RewriteRule ^goto/([^.]+)/$ $1.php
RewriteRule ^goto/([^.]+)/([^.]+)/$ $1.php?action=$2
RewriteRule ^goto/([^.]+)/([^.]+)/([^.]+)/$ $1.php?action=$2&id=$3

assuming goto/ is a faux directory. using the above, at the moment, a link to http://www.example.com/goto/page/
will indeed do what it's supposed to do (go to page.php)

however, linking to http://www.example.com/goto/page/action/
is rewriting to
http://www.example.com/page/action.php

and goto/page/action/id/ is rewriting to page/action/id.php

can anyone point me in the right direction to approach this properly, basically rewriting goto/page/action/id/ to page.php?action=action&id=id BUT allow for the fact that there may only be $1, or there may be $1 and $2 or there may be all three?

[edited by: jdMorgan at 11:38 pm (utc) on July 30, 2008]
[edit reason] example.com [/edit]

jdMorgan

11:37 pm on Jul 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> ...point in the right direction:
Read up on regular expressions. It it your incorrect regex patterns that is the likely cause of this trouble.

Any URL that starts with "goto/", does not contain a period, and ends with a slash will match the first rule, because the pattern does not reject additional slashes embedded in the URL-path.

Also, look up and use the [L] flag on all of your rules, unless you *know* a reason why you should not.

Links to tutorials and documentation on both subjects are available in our forum charter (See link top left this page). As it explains, we're here to help you make your own string longer, not to just give you more string... :) Additional specific questions are welcome!

Jim

MrMeAndMyself

11:45 pm on Jul 30, 2008 (gmt 0)

10+ Year Member



Hi Jim, thanks for the reply.

>Any URL that starts with "goto/", does not
>contain a period, and ends with a slash will match
>the first rule, because the pattern does not reject
>additional slashes embedded in the URL-path....

Actually, this makes sense; I see where you're going. Now that I have and idea where I'm going wrong; the read-ups can begin.

Thanks again.

MrMeAndMyself

9:21 pm on Jul 31, 2008 (gmt 0)

10+ Year Member



Okay, this by all accounts seems to be working (once I figgured out I had to stop using relative URLs in the site) - However, just wondering, is there a more efficient way to do this, or is this the way I should be doing it?

RewriteRule ^goto/([a-zA-Z]+)(/([a-zA-Z]+))(/([a-zA-Z0-9]+))/$ $1.php?action=$2&id=$3 [L]

RewriteRule ^goto/([a-zA-Z]+)(/([a-zA-Z]+))/$ $1.php?action=$2 [L]

RewriteRule ^goto/([a-zA-Z]+)/$ $1.php? [L]

jdMorgan

10:26 pm on Jul 31, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Looks like your "action" and "id" may go wrong, because of the nested parentheses. Not sure what you're intending there, but in the first rule, if the requested URL-path was /goto/bob/ted/bill, the resulting filepath would be "bob.php?action=/ted&id=ted". That is, "action" would have "/ted" with a leading slash, "id" would have "ted" without the leading slash, and bill would get dropped (ignored).

You may have meant:


RewriteRule ^goto/([a-zA-Z]+)/([a-zA-Z]+)/([a-zA-Z0-9]+)/$ $1.php?action=$2&id=$3 [L]

...but I can't be sure.

Jim

MrMeAndMyself

10:02 am on Aug 1, 2008 (gmt 0)

10+ Year Member



Eh, yeah, that is what I ment. I just tested it to landing pages, didn't notice the vars weren't what they were supposed to be.

Once again, thank you for your help. This will get me out a hole and let me move forward, and the sample you provided has made it abundently clear where I was going wrong. All the same, I think I still need a much longer look at regular expressions.

Again, thank you.