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 »

Ya, but inside the [], as those specify a range of characters that should be considered a match. And really, all you need is just a space, not necessarily \s
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 »

Code: Select all

DirectoryIndex index.html index.htm index.php
order allow,deny

allow from all 

Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteRule ^categ/([0-9]+)/([a-Z0-9][ ]+)/ /index.php?page=categ&c=$1&cname=$2 [L]
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]
RewriteRule ^$ index.php?page=home&menu=home [L]
This causes a Server Error on any page, even index.php?page=home.
Error 500. With or without the space.
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 »

put the space inside the [] you already have: [a-Z0-9 ].

500 errors are a pain in the butt to figure out. Best to use that regex tester I pasted a few pages back to test your expression independently.
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 »

It's definintely only this:
RewriteRule ^categ/([0-9]+)/([a-Z0-9 ]+)/ /index.php?page=categ&c=$1&cname=$2 [L]

That is causing that error - as completely removed, it works fine in the basic index.php?page=home.
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 ^categ/([0-9]+)/([a-z0-9]+)/ /index.php?page=categ&c=$1&cname=$2 [L]

It likes this, but it doesn't like
RewriteRule ^categ/([0-9]+)/([a-Z0-9]+)/ /index.php?page=categ&c=$1&cname=$2 [L]
(capital Z)
RewriteRule ^categ/([0-9]+)/([a-z0-9 ]+)/ /index.php?page=categ&c=$1&cname=$2 [L]
Nor this, with the space in it.
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 »

Hmmm, maybe you need to do [a-zA-Z0-9].

Not sure how to put the space in, if what I suggested is causing an error.
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 that works, by which I mean it doesn't cause an error - BUT,
http://test.local/categ/49/Specials

Code: Select all

DirectoryIndex index.html index.htm index.php
order allow,deny

allow from all 

Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteRule ^categ/([0-9]+)/([a-zA-Z0-9]+)/ /index.php?page=categ&c=$1&cname=$2 [L]
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]
RewriteRule ^$ index.php?page=home&menu=home [L]
Produces this:
Object not found!

The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.

If you think this is a server error, please contact the webmaster.
Error 404
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 »

Since there's no trailing slash, it's not matching your "categ" rule - must be one of the others.
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, you mean it must be one of the other's causing what?
The error I had before is now gone. It was as you suggested.
But this 'categ' rule is not working, and is causing this problem.
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 URL "http://test.local/categ/49/Specials" would not match your first rule, as the first rule requires the URL to have a trailing slash. Therefore, the error you're getting must be caused by one of the other two rules.
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 »

It's the missing slash that was causing it. Didn't realise how important it was. It works with that.
So next is for me to see if I can figure out how to add the subid and subname....
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 »

Fantastic, I've sussed it, and the Sub Category, and got it working dynamically from the side menus.

DirectoryIndex index.html index.htm index.php
order allow,deny

allow from all

Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteRule ^categ/([0-9]+)/([a-zA-Z0-9\s]+)/ /index.php?page=categ&c=$1&cname=$2&menu=sub [L]
RewriteRule ^subcateg/([0-9]+)/([a-zA-Z0-9\s]+)/([0-9]+)/([a-zA-Z0-9\s]+)/ /index.php?page=subcateg&c=$1&cname=$2&s=$3&sname=$4&menu=sub [L]
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]
RewriteRule ^$ index.php?page=home&menu=home [L]

Next job is to link the Product pages, and then put them on the Category pages themselves.
So I undertand the a-z, A-Z and 0-9, and I see what the / slashes are for, as they are part of the URL, but still not clear, even with that tutorial what the + sign means.
Can you elaborate in lamens terms please?

The \s works, as I have had words with spaces and it works. So cleaning it has to be \s rather than just a space put in there.
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 + means "match the preceeding character/range one or more times". Similarly, * means 0 or more times.

So if you have an expression like: abc8+, it would match the strings abc8 and abc888888888, but not abc. If you had abc8*, it would match abc.
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 »

. (dot) ANY single character at all

How do I put this in, as I have some product names with titles like: T-SHIRT
([a-zA-Z0-9\s.]+)
This doesn't work.
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 for the + example. I'm sure there are times when that would come into play, not sure when tho.
I'm trying to work out this hyphen in the word. I might also add this to the category part too, as someone may just put in hyphens in there, and that would kill it!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Locked