Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Short URL system: How to redirect the Custom URLs?

I'm trying to make a tinyurl-like service for my company, and so far looks good, but now I have a problem I can't solve.

Lets say the URL I generate is "www.thecompanyiworkfor.com/shorturl/2jh62/". My guess is I have to use some script, lets say "redirect.php", where I access the databank, look for that short url, find the original one, and redirect with headers.

My question is, how can i make that "www.thecompanyiworkfor.com/shorturl/2jh62/" open "redirect.php" and that I can access the "shorturl" as a parameter? I thought I would have to do something with .htaccess, but I'm not really sure what should I do...

Help please!

like image 503
Enrique Moreno Tent Avatar asked Sep 21 '11 10:09

Enrique Moreno Tent


People also ask

How do I redirect a short URL?

URL shorteners use the 301 redirects to forward browsers from a short link location to the final destination. A 301 redirect passes the link equity – or SEO value – of a short URL on to this new location.

How do I shorten a URL with a custom name?

Bitly is a powerful (and popular) tool for shortening URLs. The free service lets you shorten links using the Bit.ly domain name, while the premium service lets you use your own custom domain name. Bitly has features like link retargeting in the paid version.

How do I create a custom short URL for free?

Like TinyURL, Tiny.cc is a quick and easy free URL shortener. On Tiny.cc, you can post a long URL into the box on the front page, add an optional custom link ending, and click the button to shorten links for free.


1 Answers

Here's what I recommend.

1) Create a sub domain (s.thecompanyiworkfor.com). It will be easier to manager and you avoid conflict with .htaccess since this folder is separated from the main WWW folder.

for example:

s.thecompanyiworkfor.com => /home/thecompanyiworkfor.com/s_public_html/
www.thecompanyiworkfor.com => /home/thecompanyiworkfor.com/public_html/

2) Use this .htaccess in the /home/thecompanyiworkfor.com/s_public_html/

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !^index\.php
RewriteRule ^([a-z0-9\-]+)(\/?)$ index.php?code=$1 [L,NC,QSA]

Then in your /home/thecompanyiworkfor.com/s_public_html/index.php you can check which code correspond to which URL and redirect. If not found, redirect to www.thecompanyiworkfor.com

like image 156
Book Of Zeus Avatar answered Sep 21 '22 16:09

Book Of Zeus