wordpress - 如何使用漂亮的永久链接(SEO 友好的 URL)在 Nginx 上与 Laravel 一起安装 WordPress?

标签 wordpress nginx laravel

我有一个在 Nginx 上运行的 Laravel 网站,一切都很好。

它有一个正常的文件夹结构,例如:

/app
/public
/vendor
...

/public 文件夹是 Laravel index.php 所在的位置。

我已在 /public/blog 安装了 WordPress,因为我希望我的博客在 mywebsite.org/blog 上可见。

如果我将 /blog/wp-admin/options-permalink.php 中定义的永久链接设置设置为“默认”(这意味着帖子的 URL 看起来像 /blog/?p=123)。 如果我将固定链接设置更改为 /blog/%postname%/,我将无法查看帖子(我会看到 Laravel 404 页面)。

我绝对希望我的博客文章具有 SEO 友好的 URL(漂亮的永久链接)。

我当前的 Nginx 配置是:

server {
    #This config is based on https://github.com/daylerees/laravel-website-configs/blob/6db24701073dbe34d2d58fea3a3c6b3c0cd5685b/nginx.conf and seemed to be necessary to get Laravel working.
    server_name mysite.local;

     # The location of our project's public directory.
    root F:/code/mysite/public/;

     # Point index to the Laravel front controller.
    index           index.php;

    location / {
        # URLs to attempt, including pretty ones.
        try_files   $uri $uri/ /index.php?$query_string;
    }

    # Remove trailing slash to please routing system.
    if (!-d $request_filename) {
            rewrite     ^/(.+)/$ /$1 permanent;
    }

    # Yoast WordPress SEO plugin says to add these 2 rewrites:
    rewrite ^/blog/sitemap_index\.xml$ /blog/index.php?sitemap=1 last;
    rewrite ^/blog/([^/]+?)-sitemap([0-9]+)?\.xml$ /blog/index.php?sitemap=$1&sitemap_n=$2 last;

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9123
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9123;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    location ~* \.(css|js|gif|jpe?g|png)$ {
        #images, CSS, and JS have 1 week expiration: http://aspyct.org/blog/2012/08/20/setting-up-http-cache-and-gzip-with-nginx/ See also: http://serverfault.com/questions/339240/chromium-audit-says-its-not-caching-static-content-yet-headers-are-set-who-i
        expires 168h;
        add_header Pragma public;
        add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    }

}

我花了几个小时查看其他答案(如下所列),但还没弄清楚如何让它发挥作用。

建议?

附注我可以灵活选择安装 WordPress 文件的位置(例如,在 /public/blog 或将其上移到 /blog/wordpress )。

最佳答案

您将所有内容路由到 / 位置中的 laravel,但您需要将所有 /blog/ 写入 /blog/index 中的 index.php。 php:

location /blog/ {
    try_files $uri $uri/ @wordpress;
}

location @wordpress {
    rewrite /blog/ /blog/index.php;
}

那么你的 php 处理程序需要路径信息支持:

location ^/blog/index.php(/.*)?$ {
    fastcgi_split_path_info ^(/blog/index.php)(/.*)$;
    fastcgi_pass   127.0.0.1:9123;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_param  PATH_INFO $fastcgi_path_info;
    include fastcgi_param;
}

如果这不起作用,请打开错误日志的调试详细程度并发布日志信息。

更新:原始提问者的注释:

这是我的新 Nginx 配置的片段,它似乎适用于这些 URL:/、/blog、/course、/blog/innately-happy 和/blog/sitemap_index.xml

...
error_log /Users/myuser/code/myproject/storage/logs/nginx_error.log debug;

 # Point index to the Laravel front controller.
index           index.php;

location /blog/ {
    try_files $uri $uri/ @wordpress;
}

location @wordpress {
    rewrite /blog/ /blog/index.php;
}

location ^/blog/index.php(/.*)?$ {
    fastcgi_split_path_info ^(/blog/index.php)(/.*)$;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_param  PATH_INFO $fastcgi_path_info;
    include fastcgi_params;
}

location / {
    try_files $uri $uri/ /index.php$is_args$args;
}
...

关于wordpress - 如何使用漂亮的永久链接(SEO 友好的 URL)在 Nginx 上与 Laravel 一起安装 WordPress?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23645386/

相关文章:

html - 如何在移动/平板电脑 View 中在 css 的不同行上显示内容?

html - hover div的高度在增加

linux - ASP.NET Core 2 Web API - 502 Bad Gateway(Centos 7) - NGINX

python - 在 Apache 中使用多个 WSGI 挂载点和 Nginx 反向代理

javascript - Laravel Blade : Parse error: syntax error, 意外 ':' ,预期 '('

javascript - 如何使用 javascripts 对齐旋转的轮子以停止在中心位置而不是随机位置?

mysql - 进入Wordpress数据库

nginx - 如何在 NGINX 配置中的两个位置使用相同的规则?

javascript - Laravel:如何在 JavaScript 中使用 Laravel Blade?

laravel - laravel eloquent 中的条件选择语句