Yourls: Need info to update 302 plugin

Created on 12 Aug 2020  Â·  19Comments  Â·  Source: YOURLS/YOURLS

You all have recommended the 302 plugin several times

https://github.com/EpicPilgrim/302-instead/blob/master/plugin.php

However, it doesn't seem to work. What do I need to update to get it to work with the latest version? I will update and post PR.

plugin domain question

Most helpful comment

@dgw is such a support champ :)

All 19 comments

"it doesn't seem to work" is very vague. Is there an error? Does it just get ignored (and links redirect with default status 301)?

You can see the relevant action and filters here: https://github.com/YOURLS/YOURLS/blob/c1517f1f84baf55e76db0f97814ceb49ffd34bfa/includes/functions.php#L232-L234

In modern YOURLS it's probably better to filter the redirect_code than for the plugin to redirect itself. That would be a good PR for you to make! (I'm not involved with that plugin's maintenance, of course. This is just my opinion. :)

Does it just get ignored (and links redirect with default status 301)

Yes.

I'm new to the codebase, but I will try. What RTFM material should I look to for help?

Start by reading the Actions and Filters sections in our wiki page for developers. There's actually example code there that relates to custom redirect status codes.

There might also be some configuration detail of your setup that prevents YOURLS from altering the default status because the headers get sent too soon (i.e. output buffering is turned off in PHP), but I haven't dug into that yet.

This is what I have come up with but it doesn't work. How can I debug?

Thanks.

https://gist.github.com/tinjaw/74168463984fc713c7440b45aa31cbe0

BTW FYI, the main purpose of my YOURLS site is to allow downloading the latest version of a file using the same URL.

Ah, hmm. I think the redirect_code filter expects just the code to be returned by filters; the $location ($args[1]) is provided only for context. Your gist tries to return multiple values.

Changing the redirect location is its own filter, redirect_location, which receives the $code as context despite no filters being able to alter the code yet. (Wait a sec… @ozh, is that really useful? 😹)

I updated the gist, but it still doesn't work

The function must either accept both arguments, or register using the accepted_args argument to yourls_add_filter() to specify how many of the filter's arguments it expects. Try:

function 302_redirection( $code, $location ) {
    return 302;
}

(All of this is kind of guesswork on my part because I haven't worked with YOURLS plugins in a while, and can't currently try stuff myself to see what works.)

Also, PHP identifiers can't start with a digit. Try naming the filter tinjaw_302_redirection instead.

Updated gist. Still not working.

Am I correct in assuming the PHP file gets parsed every use? Or do I need to do something to force a "refresh"? This particular instance is on a hosting provider.

The PHP file should, yes. But assuming the code works now, if your browser or any caching/CDN layer in between YOURLS and your client has fetched a 301 link once, when the plugin wasn't working, it probably won't ever go back to the origin server during testing. One must jump through some hoops to disable all caching, or test each iteration with a different shortlink name to bypass any cached redirects.

I have been testing unused URLs. And I can create a new one for testing. I will now, but I don't think things are working properly.

Just created a new link and I get 301.

Sorry, I really didn't think this would take up much of your time. I appoligize.

Since we've reached the point of checking _everything_ just in case: You've made sure to activate this plugin from <your_domain>/admin/plugins.php, right?

And it's still possible that your server or PHP configuration is sending the headers before this filter gets a chance to run, meaning the status code can't be overridden in time. Check PHP's output buffering settings, for a start.

Since we've reached the point of checking _everything_ just in case: You've made sure to activate this plugin from <your_domain>/admin/plugins.php, right?

That was it !!! Doh! Thankfully it was just that simple.

OK, thank you so much for your help. I will make this all look nice and pretty and ready for primetime and then I will formally announce it as publicly available. And you can start sending people to my plugin if they want 302.

If I find the time, I will add some logic for choosing between 301 and 302.

Thank you so much.

@dgw is such a support champ :)

Now public as a Gist.

https://gist.github.com/tinjaw/5da8ef61d91b73ca928256bb51200117

Please link to it if you feel it is worthy.

Thanks again for your help @dgw & @ozh

@tinjaw Proposed: YOURLS/awesome-yourls#70 😸

Was this page helpful?
0 / 5 - 0 ratings

Related issues

uusijani picture uusijani  Â·  3Comments

infotek picture infotek  Â·  7Comments

gazzola picture gazzola  Â·  7Comments

sshcli picture sshcli  Â·  3Comments

dylanh724 picture dylanh724  Â·  6Comments