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 »

It isn't.

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

Options +FollowSymLinks
Options +Indexes
RewriteEngine On
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]
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&h=$6 [L]
RewriteRule ^([^/\.]+)/?$ index.php?page=$1&menu=home [L]
RewriteRule ^$ index.php?page=home&menu=home [L]
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're going to drive yourself slowly insane doing it this way. I have to suggest again that you look back at the method I mentioned earlier using the 404 handler and a database.
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 Eric, but absolutely no way can I do it that way.
This way works brilliantly, specially with hyphens.
I just need to be able to run the pagenum part (second rule), and then I am sorted!
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! »

Why not? It's much cleaner and easier to maintain than 6+ lines of rewrite rules. Especially a year from now when you need to make changes to it and have to try to relearn what all those lines are doing all over again.
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 »

These are all the lines I need.
Having spent two days writing all this and updating a massive web site, and with 'pickles' help - sorry no.
Just need to know what I am doing wrong with that pagenum example.
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 »

Can you not tag things on the end of a ShortUrl?
/PRODUCT/BIG-RED-SHIRT/&sn=yes
Here is my URL for a shirt. It's posted to an external site and then comes back with the "&sn=yes" which the page should see as stating something has been sent, and disables the form from being used again.

So how do you tag something on the end of a shorturl, and store it it a variable? I assumed it would take $sn as a variable and put "yes" into it.

But it doesn't.
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! »

simonmlewis wrote:Can you not tag things on the end of a ShortUrl?
/PRODUCT/BIG-RED-SHIRT/&sn=yes
This is one of the problems with the rewrite technique. It is going to modify your URL's and in some cases drop data. I know I'm a broken record, but if you handle the short links with PHP code and a database you'll have easy 100% control. As a bonus you'll also see valid missing links show up, missing external links (which you can properly redirect), and monitor your short-URL traffic directly.

Perhaps you could modify your regex to pass on all the URL parameters to whatever page you are rewriting to by ignoring them, but I would have to spend a fair bit of time experimenting with it to fix your problem. I don't know off the top of my head.
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

NameVirtualHost *
  <VirtualHost *>
    DocumentRoot "C:\xampp\htdocs"
    ServerName localhost
  </VirtualHost>
  
  <VirtualHost *>
    DocumentRoot "C:\xampp\phpMyAdmin\test1"
    ServerName test1domain.local
  <Directory "C:\xampp\phpMyAdmin\test1">
    Order allow,deny
    AllowOverride All
    Allow from all
  </Directory>
  </VirtualHost>
  
  <VirtualHost *>
    DocumentRoot "C:\xampp\phpMyAdmin\test2"
    ServerName test2domain.local
  <Directory "C:\xampp\phpMyAdmin\test2">
    Order allow,deny
    AllowOverride All
    Allow from all
  </Directory>
  </VirtualHost>
I've added test2 as a new virtual host, so I can have a second domain virtually hosted, but when I go to that domain, it just tries to take me to http://www.test2domain.local....

It says "Server not found" so it's not a HTACCESS error.
What have I done wrong with this config file??
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 »

That's both a new, and an Apache config question, so it should be asked in the System > Installation & Configuration forum.
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 »

Don't worry it was simple: the HOSTS file wasn't updated. How about this problem tho:

RewriteEngine On
RewriteRule ^categ/([0-9]+)/([^/]+) /index.php?page=categ&c=$1&cname=$2&menu=sub [L]
RewriteRule ^categ/page/([0-9]+)/([^/]+)/([0-9]+) /index.php?page=categ&c=$1&cname=$2&menu=sub&pagenum=$3 [L]
RewriteRule ^subcateg/([0-9]+)/([^/]+)/([0-9]+)/([^/]+) /index.php?page=subcateg&c=$1&cname=$2&s=$3&sname=$4&menu=sub [L]
RewriteRule ^subcateg/page/([0-9]+)/([^/]+)/([0-9]+)/([^/]+)/([0-9]+) /index.php?page=subcateg&c=$1&cname=$2&s=$3&sname=$4&menu=sub&pagenum=$5 [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&h=$6 [L]
RewriteRule ^productsall/([0-9]+)/ /index.php?page=productsall&pagenum=$1/ [L]
RewriteRule ^sml/([^/]+) /index.php?page=a_login [L]
RewriteRule ^([^/\.]+)/?$ index.php?page=$1&menu=home [L]
RewriteRule ^$ index.php?page=home&menu=home [L]

I need the "sml/" rule to show index.php?page=a_login.
Even tho other /fred rules will show 'page=fred'. So basically that rule should override the other rule.

How do I do that?
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 am not very confused.
If a link goes to http://www.domain.co.uk/videos
It takes them to:
videos/?page=videos&menu=home
But if they go to /productsall, /contact, /faq .... etc, they all just show /productsall.... etc in the URL.
But /videos goes to that extended URL.

There are no rules in my HTACCESS that controls it. The problem is, when that page is up, and they click a link in the side menu that goes to /categ/55/fred..... it actually goes to /videos/categ/55 .....

Why would one page show a different url?

My HTACCESS file:

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]+)/([^/]+) /index.php?page=categ&c=$1&cname=$2&menu=sub [L]
RewriteRule ^categ/page/([0-9]+)/([^/]+)/([0-9]+) /index.php?page=categ&c=$1&cname=$2&menu=sub&pagenum=$3 [L]
RewriteRule ^subcateg/([0-9]+)/([^/]+)/([0-9]+)/([^/]+) /index.php?page=subcateg&c=$1&cname=$2&s=$3&sname=$4&menu=sub [L]
RewriteRule ^subcateg/page/([0-9]+)/([^/]+)/([0-9]+)/([^/]+)/([0-9]+) /index.php?page=subcateg&c=$1&cname=$2&s=$3&sname=$4&menu=sub&pagenum=$5 [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&h=$6 [L]
RewriteRule ^productsall/([0-9]+)/ /index.php?page=productsall&pagenum=$1/ [L]
RewriteRule ^([^/\.]+)/?$ index.php?page=$1&menu=home [L]
RewriteRule ^$ index.php?page=home&menu=home [L]
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
munkitkat
Forum Newbie
Posts: 6
Joined: Thu Mar 10, 2016 6:22 am

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

Post by munkitkat »

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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post by Celauran »

simonmlewis wrote: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.
munkitkat wrote: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.
Copypasta + thread necromancy = locked.
Locked