Page 2 of 6

Re: Short / Tiny URL - how do you do it, and how can it work

Posted: Mon Jan 09, 2012 10:04 am
by simonmlewis
Yes I can get /page/selector to work.
But, I don't want "page/selector" I want just "/selector/".

And given the product example, you have read it slightly wrong. I want to convert that long url to "/product/657/", but cannot see how to do that.

ALL my pages used index.php as the template. 100% of them. But I don't want to have to use /index/page/product/657, if the url was index.php?page=product&product=657.

Re: Short / Tiny URL - how do you do it, and how can it work

Posted: Mon Jan 09, 2012 10:07 am
by pickle
So change your regex then to not have /page/ in it, just:

RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]

Re: Short / Tiny URL - how do you do it, and how can it work

Posted: Mon Jan 09, 2012 10:12 am
by simonmlewis
Fantastic. Works!
Ok, so from there, if I wanted to go straight to a category using both the category ID and the category name, how would I do that?
It must be slightly similar to this, since I want the URL to be something like:
/category/434/tshirts/
And the full hidden url would be:
/index.php?page=categ&menu=sub&c=49&cname=shirts

There ar esub catergories too, but if I can work out categories, I'm sure I can suss out the subs too.

Re: Short / Tiny URL - how do you do it, and how can it work

Posted: Mon Jan 09, 2012 10:15 am
by pickle
Make a new rule before your existing rule that catches urls starting with /category/ - much like your /page/ expression did.

Re: Short / Tiny URL - how do you do it, and how can it work

Posted: Mon Jan 09, 2012 10:18 am
by simonmlewis
Ok, so something like this:
RewriteEngine On
RewriteRule ^$ index.php?page=home&menu=home [L]
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]
RewriteRule ^categ/([^/\.]+)/?$ index.php?page=$1 [L]

....but how do I get the catid and the catname in there?

Re: Short / Tiny URL - how do you do it, and how can it work

Posted: Mon Jan 09, 2012 10:23 am
by pickle
Before your existing rule.

Write the regex to match the URL structure you want. You get the catid & catname into the URL by writing the regex to match to a URL that is structured with the catid & catname in them.

And you probably want to redirect to something like index.php?catid=$1&catname=$2 (there's a hint for you).

Re: Short / Tiny URL - how do you do it, and how can it work

Posted: Mon Jan 09, 2012 10:25 am
by simonmlewis
I'm sorry, but when you said "write the regex to match the URL structure", and then "the hint" you totally lost me. I'm completely blind to this. Trying to read up on it, but it's completely Greek to me.
I'm looking for answers, but an explanation with the answer so I can stretch my mind and learn from it, and expand.

I've had the odd person say "search for XX".... and that just means nothing. Sorry.

Re: Short / Tiny URL - how do you do it, and how can it work

Posted: Mon Jan 09, 2012 10:30 am
by pickle
Didn't you write the first pattern? Do you know how to do regex at all? (not meant to be a snide comment, just inquisitive)

Re: Short / Tiny URL - how do you do it, and how can it work

Posted: Mon Jan 09, 2012 10:35 am
by simonmlewis
No I don't. I wouldn't even call myself a Novice yet, pickle.
It's something I want to learn, but as I said to a friend:
if you want learning the way from A to D, and something said "just try B first".... it would be of no help. you need to be told the way, the route from A to D, and to learn of the problems and methods in between the junctions.

If that makes sense. So please, treat me like a dumb programmer, coz at the moment with mod_rewrite, that is what I am!!

Should this work?
RewriteEngine On
RewriteRule ^$ index.php?page=home&menu=home [L]
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]
RewriteRule ^categ/([a-f0-9]+)?(.*)/([a-f0-9]+)?(.*)/ /index.php?page=categ&c=$1&cname=$2 [L]

Enter: /categ/49/Shirts/
It doesn't.

Re: Short / Tiny URL - how do you do it, and how can it work

Posted: Mon Jan 09, 2012 10:43 am
by pickle
Ok, you need to move your rules around. Your first rule should be matching everything, but you want it at the end to catch anything your other rules don't. Your first rule should be the "categ" rule, then your generic index.php?page rule, then your index.php?page=home&menu=home rule.

To find out more about how regex works, please read through the crash course we have posted here: viewtopic.php?f=38&t=33147

Re: Short / Tiny URL - how do you do it, and how can it work

Posted: Mon Jan 09, 2012 10:46 am
by simonmlewis
Ok thank you. I have adjusted as you advise.
What about my issue of a categ rule. Or should that work in the way I described it now?... because it isn't.

Re: Short / Tiny URL - how do you do it, and how can it work

Posted: Mon Jan 09, 2012 10:55 am
by pickle
No, it won't work as you've written it - too many parenthesis, and ? and .*, etc. Read the crash course & you might get a better grasp on how to set up your expression.

Re: Short / Tiny URL - how do you do it, and how can it work

Posted: Mon Jan 09, 2012 10:58 am
by simonmlewis
Can you show me how it should be, and explain how it works?
I have spent all day trying to work this out, and got as far as you can see!

Re: Short / Tiny URL - how do you do it, and how can it work

Posted: Mon Jan 09, 2012 11:05 am
by pickle
I realize how daunting regex can be, but you've got to help yourself first. Everything you need to know is explained in that link I've pointed you to twice.

Re: Short / Tiny URL - how do you do it, and how can it work

Posted: Mon Jan 09, 2012 11:08 am
by simonmlewis
Ok. Well I have read a lot of things today, and no further. So I will end today, still no further.
I had hoped I'd find someone here who knows the answer, could tell me, and explain it briefly and concisely, rather than my searching thru reams, and then still being utterly confused - which is what has happened to me today.