A Free And Simple URL Shortener
When sites like Bit.ly and Ow.ly are present, why would you want to build a custom short URL? Especially when they offer analytics?
For one, they are insanely expensive. Bit.ly for example offers a basic plan at $29/month. Which is a lot.
This is a completely free to use and simple tool that lets you create Short Url Links for your custom Domains.
This module is distributed via npm packet manager, which is contained within node and should be installed as one of your project's devDependencies
:
npm install --save-dev netlify-shortener
- Create a GitHub Repository and initialize it with a
_redirects
file.
This _redirects file should contain links to all the websites for which you want to create a short URL in the below format :
/go http://google.com #Custom URL /gh http://github.com #Custom URL /* https://your-website.com #Default Link
You can now modify the contents of your "_redirects" file to create your own custom link.
-
Create an account on netlify. Log in and select "New site from Git". Select GitHub and give it permissions to access your GitHub repository. Now, choose the GitHub repository that you created earlier and click on deploy.
-
Click on your site and navigate to domain settings. Here, choose your custom domain. You can either buy a new one or create a short netlify domain for free.
Netlify will automatically deploy the result when you push to this repository.
Once you are done with this. You can manually create your own short URL by editing the contents of _redirects
file.
Following the above example,
yourdomain.com/gh #should open github.com yourdomain.com/go #should open google.com
However, you can also automate this with a few simple steps.
- Go to your working directory(Your repository), and in your terminal type :
npm init -y
This should create a package.json file.
-
In your terminal enter :
npm install --save-dev netlify-shortener
And this should add netlify-shortener to your node_modules.
- You will have to edit your package.json file to include netlify-shortener in scripts and modify your baseURL
{ "baseUrl": "https://svj.netlify.app", #Modify to use your custom address. "scripts": { "shorten": "netlify-shortener" #Add netlify-shortener to scripts. } }
You are all set and ready to go.
Open your terminal and run this :
npm run shorten # simply formats your _redirects file npm run shorten https://yahoo.com yh # adds yh as a short URL for you.
If you have finished the above steps, yourdomain.com/yh
should lead you to Youtube.
The netlify-shortener
does a few things:
- adds the URL to the top of
_redirects
- runs a git commit and push (this will trigger netlify to deploy your new redirect)
- Copies the short URL to your clipboard
To contribute to URLShortener, fork the repository, create a new branch and send us a pull request. Make sure you read CONTRIBUTING.md before sending us Pull requests.
Also, thanks for contributing to Open-source!
URLShortener is under The MIT License. Read the LICENSE file for more information.