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:

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

Post by simonmlewis »

We need to setup shorturls for our longer ones.

For example:
http://wwww.domain.co.uk/index.php?page ... b&head=JJP Hats

It would be fab for this to be linked from: http://www.domain.co.uk/JJPHats

I've read a bit about mod-rewrite, but seems a serious minefield and would takes months of reading I think.
Also found a method with number, like Youtube uses, but Google adwords hates it.

I use to administrate (not design) a web site that you created a page, enter (JJPHats) into a "shorturl" <input> field, and that was it's URL, but how on earth does it know all the URL above that it's masking.

Any help would be great as I am utterly lost and completely new to this angle of programming.

Simon
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

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

Post by Eric! »

There's probably a lot of ways to do this, but I've made a little database driven tool to handle 404 pages.

For example someone links domain.com/MyURL.html but it is really supposed to be domain.com/Myurl.html. I tell the .htaccess file to redirect 404 errors to my script [text]ErrorDocument 404 /my-404-handler.php[/text] My script then analyzes incoming requests that fail. I can run it through a series of checks, like case, or typos and try to redirect them automatically to the correct page. It logs everything to a database: requested_link, redirect_to, hits, last_hit ... etc. So I can see what is getting requested and how the script is dealing with the traffic.

Likewise I also use it to log links that were unable to remap into a database. So I can go into the database and put in a hard link in the redirect_to field. For example someone tries to find A12345. They would be directed to 404 page that says the error has been recorded. I can go into the database and put in a link to redirect it to. Say for example I want A12345 to point to /My_Photos/Summer_Adventure7_10_2011.gif. I can just go into the database and put that in for a redirection. Now when the 404 error occurs on A12345 and my script takes a look at the requested URL, it knows to remap it to the photo.

This give me control of seeing how many people are using the short-cut links and the ability to point them to different URLs if I want. It also lets me see how many 404 errors I get for specific links and allows me to use the users activities to fix any missing link problems that pop up.

You can also use pretty URL's in a smart way so you can parameterize the request on the fly with your 404 handler so you don't need a database entry for every pretty URL. For example /photos/costa_rica/Spider_Monkey could be smartly parameterized to /photo_gallery.php?area=photos&country=costa_rica&animal=Spider_Monkey ... etc.
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 »

So do you literally have to put the full URL into the database, along with the HTML fake page identified in the htaccess, into the database?
What happens if you have to change a page - for example, if a product is being moved from "shopA" to "shopB", and the catid goes from 5 to 22?
the URL will clearly change, but how do you change that in the database?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

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

Post by Christopher »

simonmlewis wrote:So do you literally have to put the full URL into the database, along with the HTML fake page identified in the htaccess, into the database?
What happens if you have to change a page - for example, if a product is being moved from "shopA" to "shopB", and the catid goes from 5 to 22?
the URL will clearly change, but how do you change that in the database?
Yes, you would need to have the full URL associated with the identifier stored somewhere. I would recommend having a tool that allows you manage these entries.
(#10850)
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 similar to an idea I had, the only problem with it is that google hates URLs without the name of the product in it.
So "baseball cap"... if the URL was www.domain.co.uk.handler.php?v=34mq8934md then Google would reduce the ranking.

I've seen other methods that mean the url is something like:
http://www.domain.co.uk/baseball_cap/

This is what I want to know about.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

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

Post by Christopher »

simonmlewis wrote:This is similar to an idea I had, the only problem with it is that google hates URLs without the name of the product in it.
So "baseball cap"... if the URL was http://www.domain.co.uk.handler.php?v=34mq8934md then Google would reduce the ranking.

I've seen other methods that mean the url is something like:
http://www.domain.co.uk/baseball_cap/

This is what I want to know about.
Well, there is not much difference in doing a query for "34mq8934md" or "baseball_cap" so you may want to associate a short name with every record you want to search for. If the names are long then sometimes they combine both for URLs like http://www.domain.co.uk/baseball_cap/34mq8934md/.
(#10850)
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 »

But how do you do that without having a folder with *.html in it.
I'm absolutely dumb when it comes to achieving this.
I can sort of understand how to do it with a handler PHP file, but the filename will never show the name of the product.
It's the TinyURL tool better than this? Tho I know from reading about it in HTACCESS files, it's a bugger.

Btw... how do you make HTACCESS run of a database??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

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

Post by Eric! »

You just tell put one line in the HTACCESS file:
[text]ErrorDocument 404 /my-404-handler.php[/text]

Then in your my-404-handler.php it looks at the $_SERVER['REQUEST_URI']. Here is a brief snippet of what that php file might look like:

Code: Select all

// sanitize user input with filters and escape it
if (isset($_SERVER["REQUEST_URI"]))
    $requested = mysql_real_escape_string(filter_var($_SERVER["REQUEST_URI"], FILTER_SANITIZE_URL));
else
    $requested="";
if (isset($_SERVER["REDIRECT_STATUS"]))
    $status = mysql_real_escape_string(filter_var($_SERVER["REDIRECT_STATUS"], FILTER_SANITIZE_STRING));
else
    $status="";

// make a routine to query your database
// you can look for exact query matches
// example:  REQUESTED URI = some_product/buy_me
// db shows:  "some_product/buy_me" should be mapped to "buyers/proccess.php?item=33401&purchase=yes"
// so php script redirects to proccess.php?item=33401&purchase=yes

//if no exact match is found, you can store the requested URL in the database and flag it
//so you can go back in and remap it to something
//until you remap it, redirect them to a generic 404 page that says sorry we'll fix it soon, yada yada.

//You can also do partial matches or do other clever things to help redirect the user to the right page
//including populating your site's search page with their query to help them find what they are looking for
The .htaccess file has nothing to do with your database. Your database is accessed every time someone goes to your site and requests a page that doesn't exist (like a shortURL that you have made up). The my-404-handler.php then accesses the database and looks for the missing page by matching it to the database map list. You have to set up the DB entries for your short-urls to point to the right url. As suggested you might want to make a little PHP tool to manage your database link maps.

You can also track IP's, number of times a specific request is hit, etc.

Hopefully this helps some.

Eric
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 trouble with making this handler a 404 error handler is that the client gets sent the 404 error. Some bots will just stop there and record the fact that the page doesn't exist. My redirect script redirects absolutely all requests that aren't requests to actual files, to my handler. This prevents the 404 header from being sent and to the client, everything seems to work normally.

Code: Select all

RewriteCond %{REQUEST_FILENAME}  !-s
RewriteCond %{REQUEST_FILENAME}  !-d
RewriteRule .* index.php
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

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

Post by Eric! »

The trouble with making this handler a 404 error handler is that the client gets sent the 404 error.
That's not necessarily true. You can control the error codes and use 302 (which seems to be the default with my Apache configuration so I didn't have to do any extra header() configuration). Here's a live example and the headers. The requested /help/myUrl does not exist, but the 404 handler finds a map to /therealpath/Login.php in the database:
[text]
http://www.dumbdomain.com/help/myUrl
GET /help/myUrl HTTP/1.1
Host: http://www.dumbdomain.com
User-Agent: Mozilla/5.0 (Ubuntu; X11; Linux i686; rv:8.0) Gecko/20100101 Firefox/8.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Cookie: logintheme=cpanel; cprelogin=no; cpsession=Yn_blahblahblah

HTTP/1.1 302 Found
Date: Fri, 09 Dec 2011 18:11:30 GMT
Server: Apache/2.2.19 (Unix) mod_ssl/2.2.19 OpenSSL/0.9.8e-fips-rhel5 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635 PHP/5.3.6
X-Powered-By: PHP/5.3.6
Location: http://www.dumbdomain.com/therealpath/Login.php
Content-Length: 0
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html
[/text]

The 302 message will help search engines and bots map to the right link the next time so they aren't going through the 404 handler every time. A 301 message however might be a little bit more accurate.
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 got htaccess finally working, thanks to another thread on here.

Code: Select all

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

allow from all 

Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteRule ^$ index.php?page=home&menu=home [L]
RewriteRule ^page/([^/\.]+)/?$ index.php?page=$1 [L]
This doesn't work tho.
The page to load is: index.php?page=selector
If I enter:
http://testing/page/selector/
This just brings up a page, but no template (index.php).

So have I missed something here??
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 ^page/([^/\.]+)/?$ index.php?page=$1 [L]
This doesn't work because I think it is looking for images and stylesheets in the 'page' directory, I think.
I could adjust all images to cover this, but that would break if I had "/categories/shirts/red" wouldn't it??

How do you manage images and source files?

It's looking for images in:
/page/selector/images/flag_gb.png
But they are in "images", not in a sub folder under selector.
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 add a couple conditions that make the rule only apply if the requested URL isn't an actual file or directory:

[syntax]RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-d[/syntax]
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 »

Thanks. What about this issue of just showing one / element?
so if the URL is : /index.php?page=product&product=657
How do I make it show: /product/657/
I've tried various methods, and none work. Tried some online ones, but they do /index/page/product/product/657..... which i don't need.
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 »

I believe your second rule isn't ever getting hit, and I think you're thinking about this a little wrong.

The URL wouldn't be /index.php?page=product&product=657, the URL would be /product/657/. What mod_rewrite is for in this case is for telling the server: If a URL is requested that matches this regex (...), show the user this page instead (...).

Try removing your first rule, then if you request /page/inventory, you should get the result of loading index.php?page=inventory
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Locked