Plugin for YOURLS: replacetext

March 7, 2024 ยท View on GitHub

What for

This plugin allows you to create dynamic or smart redirects - not 100% sure what to call them, but you can use regex, simple text replacements or a script!

How to install

  • In /user/plugins, create a new folder named replacetext
  • Drop these files in that directory
  • Go to the Plugins administration page and activate the plugin
  • Have fun

How to use

A short URL keyword prefixed with regex/, followed by a regex pattern will allow you to redirect based on regex pattern, but also do replacements within the Long URL based on the pattern match. You can use numerical tokens like [[1]], [[2]], etc to insert specific match groups from the regex pattern. Prefix the number in the token with ! to force uppercase the replaced text, ie [[!1]].

A short URL keyword in the format wordone/wordtwo will allow you to replace the token [[wordone]] in the long URL whatever wordtwo is in your Navigation URL.

A replacement token may also be provided in a long URL, in which case a short URL navigation with that token as a query parameter will cause a replacement, ie, if a [[token]] is provided in a Long URL like https://example.com/?lookup=[[token]] with Short URL https://sho.rt/ex - then navigating to URL https://sho.rt/ex?token=12345 will redirect you to https://example.com/?lookup=12345.

A short URL keyword in the format $scriptname/ following by a regex pattern will allow you to call a script with name "scriptname.php" from the folder ./scripts/. See sample.php as an example for how the YOURLS filter hook should be structured. The requested keyword from the navigation URL, the matched Short URL keyword, the matched Long URL, and an array of the Regex matches are passed to the script.

Most easily described with examples...

Base YOURLS example: https://sho.rt

Short URLLong URLNavigation URL
what you would navigate to in your browser
Final Redirect
regex/(REQ[0-9]*)https://example.com/?ticket=%22[[1]]%22https://sho.rt/REQ0000010https://example.com/?ticket="REQ0000010"
whateverhttps://example.com/?ticket=%22[[req]]%22https://sho.rt/whatever?req=REQ0000010https://example.com/?ticket="REQ0000010"
regex/(.*)_(.*)https://www.google.com/search?q=[[1]]&tbm=[[2]]https://sho.rt/sample_ischhttps://www.google.com/search?q=sample&tbm=isch
reqhttps://example.com/?ticket=%22[[req]]%22https://sho.rt/req/REQ0000010https://example.com/?ticket="REQ0000010"
$sample/google_(.*)anything
passed to the script for use
https://sho.rt/google_hello%20worldhttps://www.google.com/search?q=hello%20world