XF 2.2 Accessing forum pages from outside forum directory

kirsty

Member
I've got a XenForo forum at sitename.com/forums/

And my own addon with routes, eg myroute/stuff that work at sitename.com/forums/myroute/stuff

But I'd like users to see these add on pages at eg sitename.com/somewhere/something.html

I can create an htaccess RewriteRule ^somewhere/something.html$ /forums/myroute/stuff [PT]

but although that fires up XenForo it gives a The requested page could not be found. (Code: invalid_route, controller: -, action: -) error where it doesn't seem to know what route to look for - I guess there just isn't one at all which makes sense - so route filters aren't helping.

I can envisage a system where I actually create the somewhere/something.html file as a php file and include the xf php file and pass the route I actually want through in it - although I had a go at just copying in the XenForo index.php and just got 500 errors so I'm not sure what the specifics of that file would be.

Is there an approach to this I'm missing?

(By the way - I don't want to put XenForo in the web root directory so that sitename.com/myroute/stuff would work directly as there's an awful lot of other stuff on the server beside the forums.)
 
Well, there's no reason you couldn't use web server rewrite rules like that. XenForo doesn't know the underlying request came from a rewrite, so if it's giving you an error about an invalid route, the internal request that's making it to XenForo is in fact invalid.

You are correct that you can't use route filters if you are trying to route to a request that will end up outside the application (XenForo) because it has no way to answer the request, so you would have to do it with web server rewrites.

If the RewriteRule isn't working as expected and the destination route is in fact valid, you have a few options to help debug the RewriteRule. There's a logging system built into Apache that allows you set a log level which can spew all sorts of info out about the request:


The error (invalid_route) that you see from XenForo is more or less what the issue is. Either /forums/myroute isn't a valid route in XenForo or the rewrite Apache is doing isn't actually to that route for whatever reason. Since the error is coming from XenForo, you do at least know the request is making it to XenForo, just not to the right route.
 
Thanks, that's helpful to know i'm at least heading down a reasonable path.

At the moment I'm looking at the var dump from xf and seeing that both have the exact same xf->request->server->REDIRECT_URL in them, though of course they have different REQUEST_URI values as one stays with the page I want to display and the other (without any redirection) stays at the same url.

RewriteRule ^competitions/closingsoon\.html$ forums/complist/closing/ [PT] is my actual rule and it looks as if it seems to be getting to the right place, looking at the logging the redirect looks good.

This is what the redirected page looks like, this is the one that tells me it has an invalid_route:
CleanShot 2023-04-19 at 15.01.57@2x.webp

and this is what I get when I look directly at forums/complist/closing - this one finds the route and correctly displays the rest of the page, the one above doesn't.
CleanShot 2023-04-19 at 15.02.21@2x.webp

I guess I need something more than the REDIRECT_URL to be correct for XenForo to pick up the route but I can't see what. Any clues as to where to look next would be appreciated.

Many thanks!
 
I'm not much help with Apache (I switched to Nginx a long, long time ago), but I do know REDIRECT_URL is something specific to Apache and isn't looked at when XenForo is resolving routes. Just need to figure out how to get Apache's mod_rewrite engine to replace the REQUEST_URI value (maybe it's as simple as a different RewriteRule flag, but like I said... I'm pretty useless when it comes to Apache these days.
 
Thanks, I appreciate the reply which gives me something to go and work on! I'll go and investigate getting the REQUEST_URI sorted out correctly if that's what's needed.

We actually have nginx and Apache together somehow, but I'm not sure how the setup works as I've pretty much ignored the server admin side of things since we last switched hosting companies - what they gave us worked fine and their support people are good so I've left it all to them.
 
Top Bottom