Forum Moderators: phranque
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]
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
>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.
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]
You may have meant:
RewriteRule ^goto/([a-zA-Z]+)/([a-zA-Z]+)/([a-zA-Z0-9]+)/$ $1.php?action=$2&id=$3 [L]
Jim
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.