A simple URL shortener for PHP

Related tags

URL php url-shortener
Overview

Shorty

Shorty is a simple URL shortener for PHP.

Installation

1. Download and extract the files to your web directory.

2. Use the included database.sql file to create a table to hold your URLs.

3. Configure your webserver.

For Apache, edit your .htaccess file with the following:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [QSA,L]

For Nginx, add the following to your server declaration:

server {
    location / {
        rewrite ^/(.*)$ /index.php?q=$1;
    }
}

4. Edit the config.php file.

Generating short URLs

To generate a short URL, simply pass in a url query parameter to your Shorty installation:

http://example.com/?url=http://www.google.com

This will return a shortened URL such as:

http://example.com/9xq

When a user opens the short URL they will be redirected to the long URL location.

By default, Shorty will generate an HTML response for all saved URLs. You can alter the response format by passing in a format query parameter.

http://example.com/?url=http://www.google.com&format=text

The possible formats are html, xml, text, and json.

Whitelist

By default anyone is allowed to enter a new URL for shortening. To restrict the saving of URLs to certain IP addresses, use the allow function:

$shorty->allow('192.168.0.10');

Requirements

  • PHP 5.1+
  • PDO extension

License

Shorty is licensed under the MIT license.

Comments
  • PHP error

    PHP error

    I'm not sure if you maintain this anymore or if you would even be willing to help me with my issue. Regardless I'll ask.

    I'm using PHP 7.4 on Ubuntu 20.04.4 using nginx to server my site. To my knowledge I have configured everything correctly. Upon testing it does display the shortened url page however it does not finish its execution, therefore the shortened url isn't actually attached to anything. Checking my error log (/var/log/nginx/error.log) I see it has produced the following exception.

    2022/06/27 23:57:33 [error] 739740#739740: *1953 FastCGI sent in stderr: "PHP message: PHP Notice:  Undefined index: q in /var/www/mysite.com/shorty.php on line 314" while reading response header from upstream, client: 172.69.68.17, server: mysite.com, request: "GET /index.php?url=http://www.google.com HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.4-fpm.sock:", host: "mysite.com"
    

    The issue stems from this line

    While I do not think this is the cause of my issues it might be relevant. The mysql user that shorty is using only has INSERT, SELECT, and UPDATE permissions within the scope of shorty.urls

    opened by omnituensaeternum 2
  • Undefined index: q

    Undefined index: q

    I followed the guide, added database and htdocs. But I still get this error.

    Notice: Undefined index: q in C:\xampp\htdocs\shorty\shorty.php on line 314 404 Not Found

    this is my project path : C:\xampp\htdocs\shorty this is my htdocs path : C:\xampp\htdocs\shorty.htdocs

    this is my config file.

    <?php
    // Hostname for your URL shortener
    $hostname = 'http://localhost.com';
    // PDO connection to the database
    $connection = new PDO('mysql:dbname=shorty_temp;host=localhost', 'root', '');
    $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    $salt = '';
    $padding = 3;
    ?>
    

    how can I fix this?

    opened by vishvega 2
  • Last PR introduced bug

    Last PR introduced bug

    My last PR introduced a bug that does not allow new URLs to be created because q is empty before that logic is processed. My next PR will fix this behavior.

    opened by simpleauthority 0
  • Home page does not show 404

    Home page does not show 404

    The home page does not show a 404 message because $_GET['url'] is unset at the time of checking. The page is blank, and an error is logged:

    2018/04/04 21:51:30 [error] 2990#2990: *21 FastCGI sent in stderr: "PHP message: PHP Notice:  Undefined index: url in /usr/share/nginx/html/v.craftblock.me/shorty.php on line 315
    PHP message: PHP Notice:  Undefined index: format in /usr/share/nginx/html/v.craftblock.me/shorty.php on line 316" while reading response header from upstream, client: 172.69.142.45, server: v.craftblock.me, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.2-fpm.sock:", host: "v.craftblock.me"
    
    opened by simpleauthority 0
  • 404 Not Found

    404 Not Found

    I always get this error when open my website

    My .htaccess: RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?q=$1 [QSA,L]

    config.php is finished too

    opened by vej-dev 0
  • Added some minor codes.

    Added some minor codes.

    One reported issue was people were having 404, that is because they couldn't setup the .htaccess

    I have added a local .htaccess file and updated the readme file accordingly.

    Also added the timezone config.

    opened by nahidacm 0
  • 404 when going onto shortened URLs

    404 when going onto shortened URLs

    Set everything up following the guide, .htaccess, confs for the domain etc all done, database sorted. When I try to add a URL it generates, but when I click on the URL it doesn't work, instead giving a 404 message.

    opened by Shoot 5
  • Key length

    Key length

    When doing: mysql -u root -p new_database < database.sql I see the error: ERROR 1071 (42000) at line 4: Specified key was too long; max key length is 767 bytes using MariaDB.

    This seems to be a limitation of the database itself, according to:

    https://answers.launchpad.net/maria/+question/241612

    Would there be any way to work around this?

    opened by nuxeh 2
Owner
Mike Cao
Mike Cao
A PHP-based self-hosted URL shortener that can be used to serve shortened URLs under your own custom domain.

A PHP-based self-hosted URL shortener that can be used to serve shortened URLs under your own custom domain. Table of Contents Full documentation Dock

null 1.7k Dec 29, 2022
URL shortener web application based on the Laravel PHP Framework.

UrlHub Warning: UrlHub is still in development, constantly being optimized and isn't still stable enough to be used in production environments. Whatev

Kei 349 Jan 4, 2023
The modern, privacy-aware URL Shortener built in PHP.

About UrlHum UrlHum is a modern, privacy-aware and fast URL Shortener built with PHP and the Laravel Framework. At the moment UrlHum is heavily under

UrlHum 622 Jan 7, 2023
URL shortener web application based on the Laravel PHP Framework.

UrlHub Warning: UrlHub is still in development, constantly being optimized and isn't still stable enough to be used in production environments. Whatev

Kei 348 Dec 23, 2022
:aerial_tramway: A modern, powerful, and robust URL shortener

?? A modern, minimalist, and lightweight URL shortener. Polr is an intrepid, self-hostable open-source link shortening web application with a robust A

Chaoyi Zha 4.6k Jan 1, 2023
A fast and powerful URL Shortener built with Laravel, VueJS, and Tailwind CSS.

A fast and powerful URL Shortener built with Laravel, VueJS, and Tailwind CSS.

Devpri 53 Dec 25, 2022
Simpler Url Shortener for Laravel

Laravel Url Shortener Install composer require magarrent/laravel-url-shortener Run migrations: php artisan migrate Configuration If you want to config

Marc Garcia Torrent 51 Dec 17, 2022
🔗 Your Own URL Shortener

Your Own URL Shortener YOURLS is a set of PHP scripts that will allow you to run Your Own URL Shortener, on your server. You'll have full control over

YOURLS 8.8k Jan 3, 2023
A URL shortener with various other utilities, backed by a custom lightweight framework.

da.gd What is da.gd? da.gd is both a URL shortener and a collection of quick-info tools written in PHP. It allows you to use curl (or any http client)

da.gd 76 Jan 2, 2023
URL - link shortener based on sqlite

link-url-shortener url - link shortener based on sqlite.

Okin On 1 Nov 12, 2021
A modern, powerful, and robust URL shortener

?? A modern, minimalist, and lightweight URL shortener. Polr is an intrepid, self-hostable open-source link shortening web application with a robust A

Chaoyi Zha 4.6k Dec 30, 2022
Um encurtador de URL's gratuito e Open source. Torne suas URL's um tanto pequenas forma rápida e gratuita

Slim Framework 4 Skeleton Application Use this skeleton application to quickly setup and start working on a new Slim Framework 4 application. This app

Kilderson Sena 30 Jun 30, 2022
Laravel based API to shorten URLs and share them easily. Redirects to the real URL by entering a short URL generated by the API

URL Shortener Requirements: PHP 7.4 or above composer node / npm Installation clone the project from the Github repository, enter the project folder,

Julio Vergara 5 Nov 20, 2021
Purl is a simple Object Oriented URL manipulation library for PHP 7.2+

Purl Purl is a simple Object Oriented URL manipulation library for PHP 7.2+ Installation The suggested installation method is via composer: composer r

Jonathan H. Wage 908 Dec 21, 2022
Laravel URL Localization Manager - [ccTLD, sub-domain, sub-directory].

Laravel URL Localization - (ccTLD, sub-domain, sub-directory). with Simple & Easy Helpers. Afrikaans Akan shqip አማርኛ العربية հայերեն অসমীয়া azərbayca

Pharaonic 2 Aug 7, 2022
Enables the possibility generating sanitized URL parts from persisted patterns.

#Persisted sanitized pattern mapping What does it do? Enables the possibility generating sanitized URL parts from persisted patterns. How does it work

Markus Hofmann 1 Apr 7, 2022
Checking an arbitrary URL for Micro-Framework HLEB

Checking an arbitrary URL for Micro-Framework HLEB The class RouteFinder is not included in the original configuration of the framework HLEB, so it mu

Foma Tuturov 1 Nov 4, 2021
A simple PHP library to parse and manipulate URLs

Url is a simple library to ease creating and managing Urls in PHP.

The League of Extraordinary Packages 351 Dec 30, 2022
Public Suffix List based domain parsing implemented in PHP

PHP Domain Parser PHP Domain Parser is a resource based domain parser implemented in PHP. Motivation While there are plenty of excellent URL parsers a

Jeremy Kendall 1k Jan 1, 2023