Lighttpd rewrite for Wordpress inside CakePHP

2019-09-07 03:48发布

问题:

I'm having problems to setup Lighttpd rewrite rule for a Wordpress instalation that is located inside a CakePHP app.

The folder structure looks like this:

var/
    www/
        app/
            webroot/
                blog/
        cake/
        vendors/

The cakePHP app works fine. If I try to access http://domain.tld, it loads the app front page. Any attempt to access a controller/action also works fine. The problem happens when I try to load any wordpress post. The url structure to access a WP post is this: http://domain.tld/blog/post/post-slug

What I have right now on /etc/lighttpd/lighttpd.conf is this

$HTTP["host"] == "domain.tld" {
    server.document-root        = "/var/www/app/webroot/"
    url.rewrite-once = (
        "/(css|files|img|js|php)/(.*)" => "/$1/$2",
        "^([^\?]*)(\?(.+))?$" => "/index.php?url=$1&$3",
    )
}

$HTTP["url"] == "domain.tld/blog/" {
    server.document-root        = "/var/www/app/webroot/blog/"
    url.rewrite-final = (
        "^/(wp-admin|wp-includes|wp-content|gallery2)/(.*)" => "$0",
        "^/(.*.php)" => "$0",
        "^/(.*)$" => "/index.php/$1"
    )
}

When I try to open a blog post, it opens a page that seems to be a broken cake action. It loads the cake default view (header and footer), but there is nothing on the main part. Which means that it is calling a controller/action. If I turn on the debug Configure::write('debug', 2) to see what is happening, I get the following error:

Fatal error: Class 'Debugger' not found in /var/www/cake/libs/i18n.php on line 107

On any other part of the app the debugger works fine.

There are only two ways to access a WP post. Using the default WP permanent link settings (domain.tld/blog/?p=123) or setting the server.document-root="/var/www/app/webroot/blog/", but in this case the cakePHP app won't be available.

Note: Everything works fine if I use Apache.

回答1:

I found the solution.

Rewrite issue

First, the rewrite. It is as simple as this:

$HTTP["host"] =~ "domain.tld" {

    server.document-root = "/var/www/app/webroot/"
    url.rewrite-once = (
        "/(css|files|img|js|php)/(.*)" => "/$1/$2",
        "^/blog/(.*)$" => "/blog/index.php/$1",         // to take care of WP
        "^([^\?]*)(\?(.+))?$" => "/index.php?url=$1&$3" // to take care of cakePHP
    )
}


Class 'Debugger' issue

Now, the Class 'Debugger' not found issue.

  1. chmod 777 /var/www/app/tmp/
  2. Remove any temp file from the same folder

That is the basic that I've already done. But the problem was still there. So here is the trick. Proceed with the two steps explained above, and set the debug to zero (0). On /app/config/core.php

Configure::write('debug', 0)

Load any page or action. Then, change the debug to 1. Load the page again. Finally, change the debug to 2. Load the page and you won't see that error anymore.

Sounds crazy but it worked here. If I had set the debug to 2 on the first time I loaded the page, it would throw an error.

If that does not work, you might want to try the solution that is mentioned on many sites. Add App::import('Core', 'Debugger') before the line that is causing the error. It didn't work here, but many have said that it works.