Sky
Sky

Reputation: 4370

Permalinks in Wordpress not working with Laravel + Nginx

I have a site developed with Laravel on my main route say www.example.com/. I have configured it properly with Nginx and php-fpm. My config is below.

Then I added a blog in route /blog (www.example.com/blog/) and configured it with Nginx alias.

Now the problem is that Permalinks in Wordpress are not working. Nginx redirects to Laravel's 404 page.

For example when user enters some URL like this: example.com/blog/about, Laravel's 404 page shows up which is weird.

How can I fix this? How can I config Nginx? What's Wrong?

server {
    listen       80;
    server_name  example.com;
    root /usr/share/nginx/html/;

    location /blog {
        try_files $uri $uri/ /index.php?$args;
        alias /usr/share/nginx/blog/;
        index  index.php index.html index.htm;

        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            include        fastcgi_params;
            fastcgi_param  SCRIPT_FILENAME   /usr/share/nginx$fastcgi_script_name;
        }
    }

    location / {
        root   /usr/share/nginx/main_site;
        index  index.php index.html index.htm;

        try_files $uri $uri/ /index.php$is_args$args;

        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}

Upvotes: 2

Views: 1031

Answers (2)

Vinod Suryawanshi
Vinod Suryawanshi

Reputation: 59

Suppose you are running laravel on your server and you want to use wordpress as a blog. and you installed wordpress in blogs named folder. Then you have to do changes in 3 places

1) laravel .htaccess file
2) blogs folder .htaccess file
3) nginx configuration file
  1. Laravel.htaccess file changes add this line

    RewriteCond %{REQUEST_URI} !^/blogs/

  2. blogs folder .htaccess file add this code or modify

    RewriteEngine On RewriteBase /blogs/ # <==== CHANGED LINE!! RewriteRule ^index.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /blogs/index.php [L] # <==== CHANGED LINE!!!

  3. in nginx configuration file try modify below code in both port 80 and port 443 code block

     location /blogs {
     try_files $uri $uri/ /blogs/index.php?$args;
     set                     $base /var/www/html;
     root                    $base/public;
    
    
    
     location ~ \.php$ {
    
         fastcgi..
    
     }
    

    }

Upvotes: 0

Richard Smith
Richard Smith

Reputation: 49682

You do not need to use alias when the location matches the end of the alias path. See this document.

The try_files in location /blog needs to default to the WordPress router (/blog/index.php) and not the Laravel router (/index.php).

Try:

location /blog {
    try_files $uri $uri/ /blog/index.php?$args;
    root /usr/share/nginx;
    ...

    location ~ \.php$ {
        ...
        fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
    }
}

Upvotes: 3

Related Questions