XF 2.2 XF-powered homepage in root using nginx rewrites with forum in subfolder

mattrogowski

Well-known member
Hi,

We have a query that's arisen from a client request and would like to check if what we'd be looking to do will work. We essentially need XF to power the homepage of a site, above the directory in which XF is hosted.

The setup is that XF is installed in a folder called /forums on the server, which is where their old vB forum was situated. We need to keep it here so redirects will work, and /forums is an internal XF route.

The old vB site had a standalone PHP file in the domain web root which was modified to boot vB and work as a homepage/portal. This would work on vB as it was just flat files, but won't work with XF and a single entry point with an internal router.

What the client has suggested is using Nginx rewrite rules to proxy requests through to different internal URLs, example config below:

NGINX:
location = / {
  proxy_pass_request_headers on;
  proxy_pass https://domain.com/forums/index.php;

}
location = /forums/ {
  proxy_pass_request_headers on;
  proxy_pass https://domain.com/forums/index.php?forums/-/list;
}

As far as we understand, this will cause issues with XF's URL builder, as it's having a different URL passed through to what you're actually on in the browser, and might for example link you to /forums/-/list instead of /forums as that's what's being passed through. We don't want to go ahead with it and then find out it won't work as intended and we have no viable homepage.

So, the question is, will this sort of URL proxying work with XF's router and URL builder, or is it likely that incorrect URLs will be generated?
 
Last edited:

Mr Lucky

Well-known member
Sounds overly complicated to me ...

Why don't you just install XF in root, configure the index route as desired (page with widgets?) and adjust the vB rewrites as required?
Yes I agree it sounds very complicated - no need to install xenForo in a sub directory, also you end up with forums/forums

Edit: actually yes maybe it can be more complicated if the old vb install was in /forums
 

Mike C

Well-known member
We are attempting the get the url for forums /domain/forums and the homepage to be just at /

Im aware this is complicated but just wondering if anyone has any ideas. Appreciate the replies.
 

Kevin

Well-known member
We are attempting the get the url for forums /domain/forums and the homepage to be just at /

Im aware this is complicated but just wondering if anyone has any ideas. Appreciate the replies.
Just curious since I may be missing something obvious (what happens when browsing the forums, working on code, and watching Columbo all at the same time :LOL:)...

What's the issue of just moving XF to the forums home as Kirby suggested? Don't the normal XF vB redirects handle the change of URLs (at least I think I remember the add-on handling it)?


* EDIT: Meant 'home', not 'forums'. Moving XF to the home root would result in /forums still being available since it's the XF default route and just about any of the various XF/portal solutions could be used for the homepage.
 

mattrogowski

Well-known member
I believe the intention is to keep everything under the /forums namespace, which wouldn’t happen if XF was in the root, as domain.com/forums is an XF route and would load the index, but threads would be domain.com/threads/thread-slug.123/ instead of domain.com/forums/threads/thread-slug.123/
 

briansol

Well-known member
i don't think is a true statement:

The setup is that XF is installed in a folder called /forums on the server, which is where their old vB forum was situated. We need to keep it here so redirects will work, and /forums is an internal XF route.

put it in root.

use root-level directives to catch ./forums urls
 

Jake B.

Well-known member

Issue is that we have redirects for vBulletin that rely on XenForo being in the same location as vBulletin was in previously, which would effectively require us to rebuild the URL rewrite add-on we're using for the vbSEO plugin, which is what we're wanting to avoid

use root-level directives to catch ./forums urls

/forums is a default XenForo route, we cannot redirect everything from /forums to the root domain as it would break XF URLs
 

Kirby

Well-known member
Assuming that pages/home is the desired index route and that friendly URLs are being used:

PHP:
$_SERVER['REQUEST_URI'] = '/forums/pages/home/';
$_SERVER['SCRIPT_NAME'] = '/forums/index.php';
$_SERVER['SCRIPT_FILENAME'] = __DIR__ . '/forums/index.php';
$_SERVER['PHP_SELF'] = $_SERVER['SCRIPT_NAME'];

require('./forums/index.php');

as index.php in root should do the trick.

Though I'd really try to run XF from root ... having everything under /forums IMHO does make things kinda ugly.
 
Last edited:

Mouth

Well-known member
Issue is that we have redirects for vBulletin that rely on XenForo being in the same location as vBulletin was in previously, which would effectively require us to rebuild the URL rewrite add-on we're using for the vbSEO plugin, which is what we're wanting to avoid
Not familiar with the vbSEO plugin, but if the objective seems to be to redirect vB urls (eg. /forums/showthread.php) to XF then ...
1. Install XF in root webspace
2. Use XF Route filter to change XF's /forums/ to /forum/ - Find=forums/ Replace=forum/
3. Nginx config to redirect vB /forums/ urls to XF ...
Code:
        location = /forums/member.php {
            return 301 /members/$arg_u/;
        }
        location = /forums/showthread.php {
            return 301 /threads/$arg_t/;
        }
        location = /forums/showpost.php {
            return 301 /posts/$arg_p/;
        }
        location = /forums/viewtopic.php {
            return 301 /threads/$arg_t/;
        }
        location = /forums/forumdisplay.php {
            return 301 /forum/$arg_f/;
        }
        location = /forums/viewforum.php {
            return 301 /forum/$arg_f/;
        }
        location = /forums/ {
            return 301 /forum/;
 

Jake B.

Well-known member
Assuming that pages/home is the desired index route and that friendly URLs are being used:

PHP:
$_SERVER['REQUEST_URI'] = '/forums/pages/home/';
$_SERVER['SCRIPT_NAME'] = '/forums/index.php';
$_SERVER['SCRIPT_FILENAME'] = __DIR__ . '/forums/index.php';
$_SERVER['PHP_SELF'] = $_SERVER['SCRIPT_NAME'];

require('./forums/index.php');
as index.php in root should do the trick.

Doing this specifically we're able to accomplish with NGINX rewrites, but the issue is that XF's internal link builder will still take you to pages/home. That specifically wasn't a great example as it's not a page node I don't believe, it's a route from an add-on that has things linking to it. Technically it could be worked around by removing the default navigation tab added by the add-on, but there are other links in page contents as well as redirects (including redirecting you back to that page if you log in) and even if we did a redirect of (in this example) /forums/pages/home to / we get A LOT of double redirects.

Though I'd really try to run XF from root ... having everything under /forums IMHO does make things kinda ugly.

I do agree, however we're fairly limited because of the complex URL structure with the vBulletin plugin being used it makes the redirects fairly difficult

Not familiar with the vbSEO plugin, but if the objective seems to be to redirect vB urls (eg. /forums/showthread.php) to XF then ...

They're using a custom URL structure that includes the entire hierarchy of nodes / threads in the URL structure, which makes this a bit more difficult
 

Kirby

Well-known member
Doing this specifically we're able to accomplish with NGINX rewrites, but the issue is that XF's internal link builder will still take you to pages/home. That specifically wasn't a great example as it's not a page node I don't believe, it's a route from an add-on that has things linking to it. Technically it could be worked around by removing the default navigation tab added by the add-on, but there are other links in page contents as well as redirects (including redirecting you back to that page if you log in) and even if we did a redirect of (in this example) /forums/pages/home to / we get A LOT of double redirects.
Either I do not understand what you are trying to accomplish or it doesn't make much sense to me.

So you've got an Add-on that has a route like (taking XFMG as an example) media with an index action as well as other actions on that route.
The Add-on also has a tab with this index action and various links to that index action all over the place.

You want the index action of that route (eg. "XFMG index") to be displayed on the homepage (/) which is outside XF root (/forums[/ICODE]).
You want the tab and all links to the index action of that route point to / and canonically redirect there as well while keeping everything else in /forums.

Is that correct?
 
Last edited:

Jake B.

Well-known member
So you've got an Add-on that has a route like (taking XFMG as an example) media with an index action as well as other actions on that route.
The Add-on also has a tab with this index action and various links to that index action all over the place.

You want the index action of that route (eg. "XFMG index") to be displayed on the homepage (/) which is outside XF root (/forums[/ICODE]).
You want the tab and all links to the index action of that route point to / and canonically redirect there as well while keeping everything else in /forums.

Essentially, yes. The biggest issue we have is that it's happening at the same time as an import from vBulletin to XenForo, and we have to deal with those redirects with their non standard URL formats
 

Kirby

Well-known member
Out of XF root index.php
PHP:
require('/path/to/xf/index.php');

config.php (or event listeners, I think you get the idea):
PHP:
if (isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'] === '/')
{
    $c->extend('router.public', function (\XF\Mvc\Router $router) {
        \XF::options()->boardUrlCanonical = false;
        $router->setIndexRoute('media/');

        return $router;
    });

    $c->extend('request.pather', function ($callback) {
        return function ($url, $modifier = 'base') use ($callback) {
            if ($url === '')
            {
                return '/';
            }

            return $callback($url, 'canonical');
        };
    });
}

$c->extend('router.public.formatter', function ($callback) {
    return function ($route, $queryString) use ($callback) {
        if ($route === 'media/')
        {
            return '/';
        }

        return $callback($route, $queryString);
    };
});

Controller class extension
PHP:
public function assertCanonicalUrl($url)
{
    if ($url == '/')
    {
        if ($this->app()->request()->getRequestUri() === '/')
        {
            return;
        }

        throw $this->exception($this->redirectPermanently('/'));
    }

    parent::ssertCanonicalUrl();
}
 
Top