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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

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

Post by pickle »

I keep pointing you to that link because I know it works - its what I used to learn the basics.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post by simonmlewis »

I have just read it and tried to understand it, but it does not really help me to understand how to place that in a HTACCESS file to run these sorts of queries. I'm lost still.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post by simonmlewis »

Thank you. I'm pleased it worked for you.
I'm none the wiser as I keep pointing out, but thanks.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

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

Post by pickle »

Did you read through it? The article explains what parenthesis do - which should have resulted in you changing your expression.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post by simonmlewis »

Sorry but I didn't understand it. So no, I cannot see where I have "parenthesis" perhaps wrong. I told you I was dumb to this!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post by simonmlewis »

I can see I have a combo of a-f0-9, but no idea how this is wrong as I don't see it mentioned in that tutorial.
the first section is just numbers, like 6, 34 or 657, tho it could be four digits like 2943, and the second will always be a word or two words ("jam", or, "PJ Jam") for example.
So based on that, I see a-f will likely be wrong in the first section? But don't know what it should be.
As for the other bits with the +)?(..... etc, I haven't a clue.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

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

Post by pickle »

a-f0-9 means it will match any characters in those ranges. Having a-f in there isn't necessarily wrong per se, but it's usually best to be as accurate as possible. You can change that range to 0-9. If you know there will be 3 or 4 numbers, you can follow the range with a length specification, IE: ([0-9]{3,4}), which will match 123 and 12345, but not 12 or 123456.

Will the words just be alphabet characters & spaces? In your expression, you need to specify exactly what characters will be a match, so something like ([\w ]+) would work. The + means at least one character must match.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post by simonmlewis »

As I said, the numbers will be 1, 2, 3 or perhaps in the future, 4 digits of numbers only in the first set.
The second one will be words only. I cannot see why it would ever be a numeric character. Only Alpha. But it will have spaces in it.
Why would you want to say "at least one character must match"? Is it to stop spam?

So in my scenario of wanting: /index.php?page=categ&c=45&cname=TShirts
And the outcomes URL should be /categ/45/TShirts...
How would I get there, and what would the codes/characters mean please?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

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

Post by pickle »

The meaning of + and * are explained in that article.

Again, you're thinking backwards with regards to how mod_rewrite works. The URL is /categ/45/TShirts - that comes first. It's not an outcome of anything. The "outcome" so to speak is /index.php?page=categ&c=45&cname=TShirts. Your expression is used to match "/categ/45/TShirts" and show the user a different URL as a result: /index.php?page=categ&c=45&cname=TShirts
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post by simonmlewis »

This is hard work........................
Yes I get that. But to me, to someone that has used the full URL for a great many years, it FEELS like the other way around.
I want to convert that full url into the short url.
"How would I get there, and what would the codes/characters mean please?".
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post by simonmlewis »

([a-f0-9]+)?(.*)
So the * in there means "none or more times".
What on earth does that mean in relation to saying "any characters, alphanumerc)?

So [a-f0-9]+
Says.... as many alphanumeric characters as you wish ?
ie. ....&c=4..... or &c=4jfh23h23dh2d
But still not getting the rest, or why this isn't working.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

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

Post by pickle »

Don't think of your expression with regards to the resolved, index.php URL. Think of it relative to the user-facing URL, because that's what it will be matching. You really have to understand that, as its likely making this more difficult for you.

[a-f0-9]+ will match one or more characters in the specified range, which you specified as "a,b,c,d,e,f,g,0,1,2,3,4,5,6,7,8,9". What you want is [a-Z0-9]+ will match all lower and uppercase letters, one or more times.

So with this string "Tshirts", your pattern won't match, because capital T is outside the range of acceptable characters you specified.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post by simonmlewis »

Yes having read your second paragraph, I just realised that. And yes, many of the "cname" words will have uppercase in it.
So with that in mind, that I need a-Z which as I think you are saying, will capture a, b, c ...... z, AND, A, B, C....Z.... how do I then take that to the next stage?
The first second needs only 0-9 - NOT ALPHAs.
So I think it should be:
RewriteRule ^categ/([0-9]+)?(.*)/([a-Z0-9]+)?(.*)/ /index.php?page=categ&c=$1&cname=$2 [L]

Right? I know that's not all of it correct, but I am getting there, huh?
So if the cname was always lowercase and possibly numeric, it would be [a-z0-9]
Or if only lowercase alphas, [a-z].
Moving on then..... what about the other bits that are likely to be wrong, as I now get what you mean about those characters.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

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

Post by pickle »

You can get rid of both sets of "?(.*)"

If spaces are possible, you need to specify that by putting a space character in your range.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post by simonmlewis »

RewriteRule ^categ/([0-9]+)/([a-Z0-9]+)/ /index.php?page=categ&c=$1&cname=$2 [L]

Where do I put \s for the white space? after the second 0-9 ?
RewriteRule ^categ/([0-9]+)/([a-Z0-9]\s+)/ /index.php?page=categ&c=$1&cname=$2 [L]
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Locked