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

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've been looking at that tutorial to see if there is an option that says "anything" added in that section, tho maybe that is dangerous.
So it could be "aZ09- ".
I cannot get the - to be accepted with the \s. I think once I get that working, I can work out the rest, as I have got the product pages working, and feeding thru the catids, subid, and names. I'll be able to work thru the search, tho might just leave the search as it is.
And be very useful to use HTACCESS to prevent images from being taken too.
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 »

RewriteRule ^subcateg/([0-9]+)/([^/\.]+)/([0-9]+)/([^/\.]+)/ /index.php?page=subcateg&c=$1&cname=$2&s=$3&sname=$4&menu=sub [L]
This works for just about anything, including FRED-SMITH as a sub category name, but not if there is a full stop in there:
FRED.SMITH
And we do have those. So how do I let it accept just about anything?? (or is that dangerous?)
Needs to take upper and lower case, any numbers, underscore, fullstops and hyphens.
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 »

Bingo:
RewriteRule ^subcateg/([0-9]+)/([^/]+)/([0-9]+)/([^/]+)/ /index.php?page=subcateg&c=$1&cname=$2&s=$3&sname=$4&menu=sub [L]
RewriteRule ^product/([0-9]+)/([^/]+)/([0-9]+)/([^/]+)/([0-9]+)/([^/]+)/ /index.php?page=product&c=$1&cname=$2&s=$3&sname=$4&menu=sub&product=$5&head=$6 [L]
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 »

.* works for a match to "anything". If you do that, you'll want to follow it with a ? so the subpattern isn't greedy.
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, where do I put a ?, and what do you mean "so the subpattern isn't greedy"?
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 »

Take this pattern: :plan/(.*)/:
And this URL: /plan/234/abcd/blah/

The parenthesis will match "234/abcd/blah/" because (.*) is greedy - it will match as many characters as possible. If the pattern changes to
:plan/(.*?)/:, the ".*" is no longer greedy, and will only match characters until the character following the subpattern (in this case a /) is found. With the ? in there, the parenthesis will match "234"
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 to be dumb again, but I still don't get it.
if your URL is a correct one, why is it being greedy - don't you need that full URL?
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 »

It all depends on what you want to pull out of the URL - my example was just generic.
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 »

You said mine is being greedy. It's working, so I don't know what you mean by greedy, sorry. And your example doesn't really explain it to me.
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 »

We've found out that there are issues with IE converting spaces into "%20".

So we are now looking at using it and converting to look like HTML instead:

Such as: index.php?page=product&id=4&title=T SHIRT&c=34&cname=Large
Into...
4-T-SHIRT-34-LARGE.html.

Code: Select all

RewriteRule ([^\-]+)\-([^\-]+)\-([^\.]+)\.htm$ /$1.php?$2=$3 [L,QSA]
This looks like the sort of thing that does it, ,but going back to the previous pages of this thread, you use different rules for different links:
categ/....
subcateg/....

So how do you do this for different lengths of URLS?
As some will just be:
/34-T-SHIRT.html
and some will be:
/34-T-SHIRT-44-black-786-BLACK.html.

Is there just ONE long RULE above that you use, or do you set different lengths for each different one you have?

So you have the categ, subcateg and product rules, knowing how long each will be?

Also, how does it add the - into the product name, if the DB title is actually just "T SHIRT"?

Thank you again.
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 might add this to a new thread, as it is no longer really a TinyURL - but a HTM conversion.
RewriteCond %{REQUEST_URI} ([^\-]+)\-([^\-]+)\-([^\-]+)\.htm$
RewriteRule ([^\-]+)\-([^\-]+)\-([^\-]+)\.htm /index.php?page=$1&c=$2&cname=$3 [L,QSA]

RewriteCond %{REQUEST_URI} (.+)\.htm$
RewriteRule ^([a-z0-9_]+)\.html$ /index.php?page=$1 [NC,L]
These don't work either.
I thought the top one would take the page name, c number, cname word and put it all in, and add any hyphens where there are spaces, but it doesn't.
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 »

Code: Select all

RewriteRule ^categ/([0-9]+)/([^/\.]+)/ /index.php?page=categ&c=$1&cname=$2&menu=sub [L]
RewriteRule ^categ/([0-9]+)/([^/\.]+)/([0-9]+)/ /index.php?page=categ&c=$1&cname=$2&menu=sub&pagenum=$3 [L]
This second rule isn't working.
I need to pass through a page number for pagination. But this isn't passing the page number over.

This is the URL: /categ/$catid/$cname/$page
The number is going thru:
http://domain.local/categ/44/Accessories/2

So why isn't it working? IS my htaccess setwrong?
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 »

Remember the last slash is a character that must be matched (or not if it's not there).
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 »

Even with ..... /2/
it won't work. No errors, but doesn't recognise "pagenum" is now 2.
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 »

Why is this in there: ([^/\.]+) ?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Locked