So I've got a plugin that adds a forward slash to a URL among other things. This works perfectly for the most part, if I type a URL of:
domain.com/en/products
The plugin will redirect this to:
domain.com/en/products/
It's hard to show the exact code I use as the plugin does a lot of other localisation things at the same time. But the code basically uses $_SERVER[REQUEST_URI] to detect the URL, then adds the forward slash and redirects if necessary using header( 'Location: https://' . $_SERVER['HTTP_HOST'] . '/' $URLFromAbove ).
This all works perfectly unless the page isn't found. MODX throws the error page normally, but the forward slash never gets added to the URL. As far as I can see the plugin isn't running at all. I've tried also running the plugin with the 'OnPageNotFound' event, but that doesn't seem to make a difference.
I wouldn't be too bothered, but unfortunately because of the localisation, it causes the homepage without a forward slash (domain.com/en) to 404, so it needs fixing.
I'm fairly sure I'm coming up against a MODX thing with the plugins, but the documentation on the system events is fairly limited, hence posting on this forum. I initially thought it could be a case of 'OnInitCulture' not firing during a 404 and the headers already being sent by the time 'OnPageNotFound' was fired, but I can't find anything in the MODX error log to support this.
Any help would be greatly appreciated!
You might try a separate plugin just connected to OnPageNotFound that redirects to your desired URL. Make sure it has a higher priority (0) than any other plugins attached to OnPageNotFound.
<?php
$modx->sendRedirect("http:/yoursite.com/SomeURL/");
return;
Redirects don't always work in plugins so it might fail, but it's worth a try.
[ed. note: BobRay last edited this post 8 years, 7 months ago.]
Thanks Bob, I'm still a little confused, but that does appear to have fixed it. I just swapped out my redirect method from 'header( 'Location:' to your suggested '$modx->sendRedirect' and that appears to have fixed it.
I'm still confused because I can't seem to get conclusive results in A/B testing of this solution, but it does appear to work when I just leave the $modx->sendRedirect on.
I assume the 'OnInitCulture' event fires at a different time if the page isn't found, possibly after the headers are sent, which is why changing up the redirect message has fixed it.
I've just done some further testing and it looks like that solution didn't resolve it. Instead of redirecting, it'll stop the page loading altogether and just display a blank page. This only happens when I use a new tab, when it's an existing tab it seems to work okay! I think this is why I was confused above.
I'm even more stumped now - any idea what's going on!?
As I said, redirects are often funky inside of plugins. In my experience, if they don't work, there's no other reliable way to redirect from there.
I wonder if a plugin attached to OnHandleRequest could fix the URL.
Another way to go might be a rewrite rule in .htaccess that adds the slash to URLs that need it.