Simple PHP URL Shortener Plugin ⚡


(Santhosh veer) #1

Simple PHP URL Shortener Plugin to Cloak Affiliate Links and Other External Links :boom:

= Proper HTTP Header redirection (404 and 301 Redirection)

HTTP Header Outputs

  • 404 Error

curl -I http://localhost/url/blabla

HTTP/1.1 404 Not Found
Date: Sun, 13 Aug 2017 12:39:34 GMT
Server: Apache/2.4.25 (Unix) OpenSSL/1.0.2j PHP/7.0.18 mod_perl/2.0.8-dev Perl/v5.16.3
X-Powered-By: PHP/7.0.18
Content-Type: text/html; charset=UTF-8
  • 301 Moved Permanently

curl -I http://localhost/url/hbb

HTTP/1.1 301 Moved Permanently
Date: Sun, 13 Aug 2017 12:42:09 GMT
Server: Apache/2.4.25 (Unix) OpenSSL/1.0.2j PHP/7.0.18 mod_perl/2.0.8-dev Perl/v5.16.3
X-Powered-By: PHP/7.0.18
Location: https://forum.hellboundbloggers.com/
Content-Type: text/html; charset=UTF-8
  • Create a Sperate Folder for this Plugin on your web server’s File Root Directory
  • For Example, I am going to create a New Folder Named as “go”
  • Now we need to Create three files on the Folder

index.php
links.ini
.htaccess

  • index.php

<?php

$links = parse_ini_file('links.ini');

if(isset($_GET['url']) && array_key_exists($_GET['url'], $links)){

    // Permanent 301 redirection
    header("HTTP/1.1 301 Moved Permanently");
    header('Location: ' . $links[$_GET['url']]);
    exit();

}
else{
    header('HTTP/1.1 404 Not Found');
    echo 'Oops LInk not Found.';
}

?>
  • links.ini (add the Affiliate & External link that you want to Shorten)
hbb = https://forum.hellboundbloggers.com/
example = http://example.com

hbb - slug of the URL https://example.com/go/hbb

hbb-simpsturl1

  • .htaccess (For SEO Friendly URL)
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?url=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?url=$1 [L]

Normal URL - http://example.com/go/index.php?url=hbb
SEO Friendly URL - http://example.com/go/hbb

  • Hide this Folder from Search Engines add this below line on your website/blog robots.txt file
Disallow: /go/

Methods using this Plugin

Credit Goes to tutorialzine for this awesome Concept - Read post - I made Minor Changes on this Method

Updated Files

  • index.php (Add Proper 404 and 301 HTTP Header)
  • .htaccess

If you have doubts on this TopicReply to this Thread I will Guide you :slight_smile:


Useful Code Snippets and Functions For your WordPress Site :tada: