-
-
Save ozh/1d078e9cbd5f9757be3bb32011c48461 to your computer and use it in GitHub Desktop.
<?php | |
/* | |
Plugin Name: Keep Query String | |
Description: Adds short URL query string, if any, to the long URL | |
Version: 0.1 | |
Author: Ozh | |
*/ | |
yourls_add_filter('redirect_location', 'ozh_kqs'); | |
function ozh_kqs($url){ | |
if (yourls_is_GO()) { | |
$url = yourls_add_query_arg($_GET, $url); | |
} | |
return $url; | |
} | |
// well I think that's it |
We've been using this short plugin for a while and it seems to generally work well. However, it has the unfortunate consequence of modifying the original longurl. If the longurl contains slashes (and probably other special characters) in its query string, they will be double encoded.
@ozh, do you have any suggestions on how to fix this?
Test cases
longurl (input): https://test.com/narf blah.html?a=hello there&b=whatever/you/want
stored longurl in the database: https://test.com/narf%20blah.html?a=hello%20there&b=whatever/you/want
converted shorturl (output) without this plugin (functions as desired): https://www.test.com/narf%20blah.html?a=hello%20there&b=whatever/you/want
converted shorturl (output) with this plugin activated (double-encodes slashes): https://www.test.com/narf%20blah.html?a=hello%2Bthere&b=whatever%252Fyou%252Fwant
I'm not sure what the consequences of this are, but I managed to avoid the double encoding by duplicating the yourls_add_query_arg()
function and commenting out the line that performs the reencoding (the call to yourls_urlencode_deep()
).
Here's the full version: https://gist.github.com/rinogo/345dcfb149f1020f501b262ff45035d2
I'm not sure what the consequences of this are, but I managed to avoid the double encoding by duplicating the
yourls_add_query_arg()
function and commenting out the line that performs the reencoding (the call toyourls_urlencode_deep()
).Here's the full version: https://gist.github.com/rinogo/345dcfb149f1020f501b262ff45035d2
Thank you so much for your code. That is what I need :)
@congthien I've released an updated version of the plugin: YOURLS Keep Query String
This plugin passes the short URL query string to the long URL upon redirection.