简体   繁体   中英

nginx rewrite for blog in folder solves permalinks but not files

I have the following setup:

a domain.com website

a domain.com/blog wordpress installed in a subfolder

The Nginx configuration for this looks like this:

if (!-e $request_filename) {
    set $test P;
}
if ($uri !~ ^/(plesk-stat|webstat|webstat-ssl|ftpstat|anon_ftpstat|awstats-icon|internal-nginx-static-location)) {
    set $test "${test}C";
}

if ($test = PC) {
    rewrite ^/(.*)$ /index.php?$1;
}

This config allows the homepage of domain.com/blog to be loaded but every URL from this website returns a 404 not found because the request is routed to the index in the document root of domain.com.

Therefore I added a new piece to route correctly the requests like this:

if (!-e $request_filename) {
    set $test P;
}
if ($uri !~ ^/(plesk-stat|webstat|webstat-ssl|ftpstat|anon_ftpstat|awstats-icon|internal-nginx-static-location)) {
    set $test "${test}C";
}

if ($uri ~ ^/blog) {
    rewrite ^/(.*)$ /blog/index.php?$1;
    set $test B;
}

if ($test = PC) {
    rewrite ^/(.*)$ /index.php?$1;
}

This solved the permalink problem, yet now every other file that is requested in the page returns now a 404 error.

Eventually it worked with this version:

if (!-e $request_filename) {
    set $test P;
}
if ($uri !~ ^/(plesk-stat|webstat|webstat-ssl|ftpstat|anon_ftpstat|awstats-icon|internal-nginx-static-location)) {
    set $test "${test}C";
}
if ($uri ~ ^/blog) {
    set $test "${test}D";
}
if ($test = PCD) {
    rewrite ^/(.*)$ /blog/index.php?$1;
}
if ($test = PC) {
    rewrite ^/(.*)$ /index.php?$1;
}

Basically the previous version was not filtering file requests, hence the 404.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM