0

I have a running blog based on WP. Currently, I'm developing a new web app with Laravel which should run on the same (sub)domain. The new web app should be the new start page / homepage / document root www.xyz.xyz/ of the website but I don't want to change the current WP URL structure - I don't want to run WP in a subdirectory - so everything should stay as it is for WP and it's existing URLs beside that the start page should be served by laravel and not wordpress.

Of course, it could happen that a wordpress post and a laravel webpage were using the same slug although it shouldn't, in that case the Laravel page should be served.

I'm using nginx as web server. Has anyone done something like this before? Can it be accomplished on nginx level? Does anyone have a suggestion about how to solve this problem?

The recognition if Laravel can serve the page I'll do in the exception handler, if WP also doesn't have a page to serve it will be responsible to show the 404 - Curling the WP blog works, as long as they are not running on the same subdomain / webserver.

The main question is, how do the index.php and nginx need be configured to serve both, Laravel & WP on the same (sub)domain and running in the same directory.

/** * Render an exception into an HTTP response. * * @param \Illuminate\Http\Request $request * @param \Exception $exception * @return \Illuminate\Http\Response */ public function render($request, Exception $exception) {

    if ($this->isHttpException($exception)) {

        $statusCode = $exception->getStatusCode();

        switch ($statusCode) {
            case '404':


                if(Cache::store('file')->has('magazin-startpage')) {
                    echo Cache::store('file')->get('magazin-startpage');
                    return;
                }

                $queryParameter = str_replace($request->url(), "",
                    $request->fullUrl());
                $path = $request->getPathInfo();

                $ch = curl_init();
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt ($ch, CURLOPT_PORT , 443);
                curl_setopt($ch, CURLOPT_URL,"https://www.blog.xyz"
                    .$path.$queryParameter);
                curl_setopt($ch, CURLOPT_POST, 1);
                $blog = curl_exec ($ch);
                curl_close ($ch);
                Cache::store('file')->put('www.blog.xyz', $blog, 60);
                echo $blog;

                return;

            break;
        }
    }

    return parent::render($request, $exception);
}

Thanks.

jo182
  • 39
  • 3

1 Answers1

0

I'm not sure if there is scripting which can check the result of a query before returning the page. As such, I can think of a two options.

  1. If you know the root of the slugs for wordpress, you can direct all traffic for each of them (ie sub.example.com/category) to wordpress and traffic for other "folders" to Laravel using web.config.
  2. If you do not know the root for slugs or want to mix them, you will need to create a new index.php file which handles calling either wordpress or Laravel. This file would need to connect first to Laravel and use the router to determine if the page exists. If it does, it sends the request fully to Laravel, if not, it sends the request to wordpress.

For help with method 2 see, How can I check if a URL exists via Laravel?

You'd need to merge Laravel's public/index.php with the appropriate wordpress file. Using web.config and directory switching, you should be able to have the two installs in completely separate directories. Then you can send everything to Laravel by default and edit public/index.php to send requests that don't exist into the wordpress engine.

Community
  • 1
  • 1
Loren
  • 9,783
  • 4
  • 39
  • 49
  • thanks for your answer, i edited my initial post, the 404 recognition is already done, kind of similar to your second approach but in the error handler. Do you have any examples how such a index.php + nginx config should look like? Thanks. – jo182 Jan 15 '17 at 02:51
  • I don't have an example, but I think you should be able to use the wordpress index.php file and simply import it's contents instead of the curl request since you'll have all the files right there on your webserver. Since curl is simpler, you could have WP actually run on a different subdomain and just use curl to make it look like it's on this domain to the outside world. – Loren Jan 16 '17 at 04:05