Hal Jordan
Hal Jordan

Reputation: 31

How to NGINX Rewrite SPECIFIC PAGE for Wordpress

So I'm trying to do something unique, I've spent a few hours reading about NGINX rewrite and how to create a CATCH ALL for a specific page.

What I want to do:

mysite.com/Subdivision/ is a wordpress PAGE.

I want to be able to randomly generate (TAIL PAGES) that all by default go to the Higher up wordpress page (mysite.com/Subdivision/) :

mysite.com/Subdivision/Green-Trails
mysite.com/Subdivision/River-Oaks
mysite.com/Subdivision/Creek-Plantation

And then inside of my /Subdivision/ page, I will write script telling it what to do with "Green-Trails" and "River-Oaks", and "Creek-Plantation"

After that works, the goal is to also add other stuff like

mysite.com/Subdivision/Green-Trails/4-bdr/with-pool/

and my /Subdivision page will have settings "IF 4-bdr" is found in the Request-URI, set this. IF with-pool is found in the Request URI, Set this... This will all be done in PHP snippet codes.

Just have to get past the NGINX write hurdle.

This is my current Centmin Setup with NGINX:

    ## BEGIN WORDPRESS MOD ##
index index.php index.html index.htm;
        # enforce www (exclude certain subdomains)
        if ($host !~* ^(www|subdomain))
        {
            #    rewrite ^/(.*)$ $scheme://www.$host/$1 permanent;
        }
        # enforce NO www if ($host ~* ^www\.(.*)) {
        #       set $host_without_www $1; rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
        #}


        # unless the request is for a valid file, send to bootstrap
        if (!-e $request_filename)
        {
                rewrite ^(.+)$ /index.php?q=$1 last;
        }


location ~ /subdivision/(.*) {
    index index.php;
    try_files $uri /index.php?q=/subdivision/&$args;
}

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

## END WORDPRESS MOD ##

As you can see:`

location ~ /subdivision/(.*) {
    index index.php;
    try_files $uri /index.php?q=/subdivision/&$args;
}

`

Is where I am trying to tell NGINX to SERVE the /Subdivision/ page variable to wordpress and tell Wordpress to IGNORE the rest of the URL /Green-Trails/ /River-Oaks/, /Creek-Plantation/

But this isn't working, can someone please help me, where did I miss up?

Upvotes: 0

Views: 393

Answers (1)

Richard Smith
Richard Smith

Reputation: 49692

The problem is that WordPress does not use the query string to determine the page to display, it gets the information directly from the REQUEST_URI, which is usually set to the original URI (before any internal rewrites are performed).

The simple solution is to perform an external redirect, but that will change the URI as it appears in the browser.

What you want can be achieved by setting SCRIPT_FILENAME and REQUEST_URI explicitly:

location ^~ /subdivision/ {
    include fastcgi_params;
    fastcgi_pass localhost:9000;
    fastcgi_param SCRIPT_FILENAME $document_root/index.php;
    fastcgi_param REQUEST_URI     /Subdivision/;
}

Upvotes: 0

Related Questions