Fixed XF301VB: Categories aren't redirected properly

Steffen

Well-known member
Unless you change the setting "Create pages for categories" from disabled to enabled, the 301 redirects for categories don't work. I'm in a little hurry and this is the workaround I've come up with so far:

Diff:
diff --git a/src/addons/XF301VB/Pub/Controller/Redirect.php b/src/addons/XF301VB/Pub/Controller/Redirect.php
index f1d264ccd..21e610e04 100644
--- a/src/addons/XF301VB/Pub/Controller/Redirect.php
+++ b/src/addons/XF301VB/Pub/Controller/Redirect.php
@@ -146,6 +146,11 @@ class Redirect extends Controller
                     return $this->noPermission();
                 }
 
+                if ($content instanceof \XF\Entity\Node && $content->node_type_id === 'Category')
+                {
+                    $route = 'categories';
+                }
+
                 $extraParams = [];
 
                 if ($params instanceof ParameterBag)
@@ -156,7 +161,7 @@ class Redirect extends Controller
                     }
                 }
 
-                return $this->redirectPermanently($this->buildLink($route, $content, $extraParams));
+                return $this->redirectPermanently(str_replace('/.#', '/#', $this->buildLink($route, $content, $extraParams)));
             }
         }

(I don't understand why the second part of the diff is necessary. There must be a better way.)
 
This should be a better solution:
PHP:
if ($content instanceof \XF\Entity\Node)
{
    $route = $content->getRoute();
}
 
I've implemented the part in your second post, thanks. However, we'll need clarification on the str_replace bit as that shouldn't be required (and isn't needed in my testing).
 
In our case, without the str_replace hack the redirect URL for

https://www.computerbase.de/forum/forumdisplay.php?f=1

is

https://www.computerbase.de/forum/.#arbeitsspeicher-mainboards-und-cpus.1

instead of

https://www.computerbase.de/forum/#arbeitsspeicher-mainboards-und-cpus.1

Node 1 is a category.

It has something to do with $router->buildLink('nopath:' . $route) returning . in XF\Pub\Route\Category::build.
 
I can definitely see now that the built URL is containing /.# rather than /# though it doesn't seem to cause any issue on here or even on your own site.

https://xenforo.com/community/.#official-forums.1

Was this actually causing any sort of 404 or other error? Or were you concerned about it doing an unnecessary redirect? I've tested in Firefox and Chrome without issue and it doesn't actually seem to be a redirect, it seems to correct the URL itself.

Not saying that the router shouldn't be returning the correct URL, but I just want to clarify if it actually is broken in any situation.
 
Yeah, the additional dot doesn't seem to cause any issues besides looking strange.

(I may have assumed that this wouldn't work without actually testing it back in May.)
 
It seems to be part of the standard behaviour in the pather and I'm not sure whether it's 100% known about or not, but I'm tempted to leave it as it is.

RFC3986 specifies rules for URL normalization and I think that's why it ends up working. See the URL below, although there's a load of random dots in it, the browser normalizes it and the URL works.

https://xenforo.com/./community/././threads/./xf301vb-categories-arent-redirected-properly.147470/

So, going to shrug it off 🤷‍♂️

I bet there's at least one obscure browser out there somewhere though that doesn't handle it properly 😂
 
Top Bottom