Code Monkey home page Code Monkey logo

php-url-shortener's Introduction

Simple PHP URL shortener

Requires PHP ≥ 5.4.0 or higher.

Installation

  1. Download the source code as located within this repository, and upload it to your web server.
  2. Use database.sql to create the redirect table in a database of choice. (Do not delete the INSERT statement on line 10 as it is needed to initialize the database.)
  3. Edit config.php and enter your database credentials.
  4. For additional security through obscurity™, consider renaming shorten.php to a secret file name of your choosing and tweaking the .htaccess file (line 3) accordingly.

Features

  • Redirect to Twitter when given a numerical slug, e.g. http://mths.be/8065633451249664http://twitter.com/mathias/status/8065633451249664.
  • Redirect to your Twitter account when @ is used as a slug, e.g. http://mths.be/@http://twitter.com/mathias.
  • Redirect to your Google Plus account when + is used as a slug, e.g. http://mths.be/+https://plus.google.com/u/0/116553353277057965424/posts.
  • Redirect to your main website when no slug is entered, e.g. http://mths.be/http://mathiasbynens.be/.
  • Redirect to a specific page on your main website when an unknown slug (not in the database) is used, e.g. http://mths.be/demo/jquery-sizehttp://mathiasbynens.be/demo/jquery-size.
  • Ignores weird trailing characters (!, ", #, $, %, &, ', (, ), *, +, ,, -, ., /, @, :, ;, <, =, >, [, \, ], ^, _, {, |, }, ~) in slugs — useful when your short URL is run through a crappy link parser, e.g. http://mths.be/aaa) → same effect as visiting http://mths.be/aaa.
  • Generates short, easy-to-type URLs using only [a-z] characters.
  • Doesn’t create multiple short URLs when you try to shorten the same URL. In this case, the script will simply return the existing short URL for that long URL.
  • DRY, minimal code.
  • Correct, semantic use of the available HTTP status codes.
  • Can be used with Twitter for iPhone. Just go to SettingsServicesURL ShorteningCustom… and enter http://yourshortener.ext/shorten?url=%@.

Favelets / Bookmarklets

Prompt

javascript:(function(){const%20q=prompt('URL:');if(q){location='https://yourshortener.ext/shorten?url='+encodeURIComponent(q)}}());

Shorten this URL

javascript:(function(){location='https://yourshortener.ext/shorten?url='+encodeURIComponent(location.href)}());

License

This script is available under the MIT license.

Author

Contributors

php-url-shortener's People

Contributors

beverloo avatar mathiasbynens avatar tomayac avatar tombiscan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

php-url-shortener's Issues

Not spitting out url.

I've setup the database, changed the config file accordingly. I put the files in a subdirectory and removed Rewritebase from the .htaccess file.

I am using Wordpress and trying to spit out the url within an input value using this:
<input id="fe_text" onChange="clip.setText(this.value)" type="text" value="javascript:(function(){document.location='http://chrisburton.me/shorten?url='+encodeURIComponent(location.href)}());" />

It seems to just be outputting the javascript instead.

Support PHP via fcgi(d)

From @rodneyrehm:

if you want people to use your shortener while running PHP via fcgi(d) you should remove exit/die(). They're killing processes…

while loop will never repeat

The while loop on index.php seem superfluous. It will run exactly one time if I've got it right, it is already established that there is at least one row to return. And if there by accident would have been 2 or more the loop terminates when the code hits die() in the redirect function.

Nginx config

Thanks for the great but simple script 😄 @mathiasbynens!
I've been using it since 2012 and have migrated it from server to server over the years.

My most recent install was on a LEMP stack and I'd like to share my current rewrite config for other users.

Use this in your server {} block:

location /shorten {
  try_files $uri $uri/ /shorten.php?$args;
}

location / {
  rewrite ^/(.*)$ /index.php?slug=$1 last;
  try_files $uri $uri/ /index.php;
}

Cheers!

Installation problem

I've this domain http://url.com I want it to be a url-shortener but also functional at the same time, so people could still access the root domain. Is it possible if I install the script on sub-directory but the shortener use the root url? So the link wouldn't become like http://url.com/mths/aaa but still http://url.com/aaa

I've tried to put the script on root directory with these settings :

define('SHORT_URL', 'http://url.com/'); // include the trailing slash!
define('DEFAULT_URL', 'http://url.com'); // omit the trailing slash!

those make the site inaccessible Safari told something like "it's too many redirect so Safari can't open the site", I've made change to :

define('SHORT_URL', 'http://url.com/'); // include the trailing slash!
define('DEFAULT_URL', 'http://another.url'); // omit the trailing slash!

those still not working too resulted 404 Not Found

I just make the fresh install of the script just to test it out http://url.com/mths
I've the prompt like this:

javascript:(function(){var%20q=prompt('URL:');if(q){document.location='http://url.com/mths/shorten?url='+encodeURIComponent(q)}}());

I've tried the url on the prompt with or without sub directory, it still result 404 Not Found

I hope this make sense, could you help?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.