Nginx Wordpress Internal rewrite WITHOUT changing URL/URI - 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? I do NOT want to change the URI in the browser, this is key. Is this possible?

Related

Rewrite in Subpath 404 urls with Nginx

Currently I have a main redirect to the home of my webserver. However I would like to treat the subpaths with redirect after a 404 was not for the home and yes for the subpath, that with multiple subpaths. That not to repeat the rules, I must deal with a REGEX, however I don't know how to insert these rules and return to the current subpath.
home
www.foo.com.br
subpath
www.foo.com.br/machine
www.foo.com.br/cloud
www.foo.com.br/air
today
# define error page
error_page 404 = #notfound;
# error page location redirect 301
location #notfound {
return 301 /;
}
would like answer 404
www.foo.com.br/machine/test123
go to
www.foo.com.br/machine/
The REGEX used to pick up the first field and redirect after a 404:
error_page 404 = #notfound;
location #notfound {
rewrite ^/([\w-]*)/.* /$1/ permanent;
In your php block put the fastcgi_intercept_errors set to on
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
# intercept errors for 404 redirect
fastcgi_intercept_errors on;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
You can add rewrite rules before the return statement to match the specific subdirectories you want treated differently.
For example:
location #notfound {
rewrite ^/(machine|cloud|air)/. /$1/ permanent;
return 301 /;
}
See this document for details.

nginx redirect all URI requests with query strings/arguments to the equivalent without

I was reading around about removing query strings/arguments from all requests by way of redirection like $1?$argument to just $1.
I tried what the documentation said in adding a ? to the end of the desired rewrite in order to remove query strings and arguments, but that had no effect.
I wish to maintain current functionality, and remove query strings/arguments from all URI requests.
What is conflicting with the documentation's suggestion of appending ?, or what is the correct solution for this?
# for removing .php from all requests
location / {
try_files $uri $uri/ #extensionless-php;
}
# rewrite to remove .php
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
# deny access to php includes
location ~* /includes/(.+)\.php$ {
deny all;
}
# redirect all instances of index.php to / in its respective directory (for example, /index.php to /, and /articles/index.php to /articles/)
location ~* \.php {
try_files $uri =404;
if ($request_uri ~ ^/([^?]*?)(?:(?<=/)index(?:\.php)?|\.php)(\?.*)?$) { return 301 /$1$2; }
fastcgi_pass backend;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
I see you're using my solution from nginx php friendly URL redirection without interfering with index.php causing /index! :-)
If you also want to get rid of the whole $args / $query_string, in addition to removing index and .php, then just omit the $2 from the provided solutions; also, you can have an extra if-condition for removing the args from the rest of your URLs, too.
index index.php;
if ($request_uri ~ ^/([^?]*?)(?:(?<=/)index(?:\.php)?|\.php)(\?.*)?$) { return 301 /$1; }
if ($request_uri ~ ^/([^?]*)\?) { return 301 /$1; }

Nginx remove php extension and index from url

Hope someone can help me.
be aware; sorry for possible bad english ;)
I'm trying for hours to setup my configs so my site is accessible without php extension, but will redirect any request with extension and in addition remove the /index from URL.
i have come to the point where every extension will removed and still got parsed by php5-fpm.
here is my current code:
server {
listen 80;
server_name example.com;
root /var/www/example.com;
index index.php;
<<unrelated Modulecode>>
rewrite ^(.*).php(.*)$ $1$2 permanent; # << forces index to be appended, so not exactly wanted
#rewrite ^/index$ / permanent; >> results in infinity loop for indexpage
# location ~ ^(.*)(?<!(index)).php(.*)$ { # << even don't know how to at least except index...gwarz regex x|
# rewrite ^(.*).php(.*)$ $1$2 permanent;
# }
location / {
try_files $uri $uri/ #phprule;
}
location ~ \.php$ {
try_files $uri $uri/ 404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location #phprule {
rewrite ^(.*)$ $1.php last;
}
}
the uncommented parts are some desperate trys to get it somehow to work x<
so what i want to accomplish:
example.com/index.php => example.com/
example.com/foo.php => example.com/foo
example.com/some/path/foo.php => example.com/some/path/foo
hope you will get the idea.
Probably found the issue or moved on, but for the benefit of those who come along after, these little changes did the trick for me:
location #phprule {
rewrite ^/(.*)$ "/$1.php" last;
}
/ added to the regex, quotes and a forward slash added to the rewrite string.

WP & Nginx: redirect post URLs containing '/folder/' but not image URLs containing '/folder/'

On a Wordpress / Nginx site, I am removing / redirecting a section of articles from our website, and would like to redirect all urls of this content to another section of the site.
I've got this much working just great (see below), but unfortunately the name of an image folder also contains the string I'm using to catch the redirect.
We want for these images to show still. Here are a few example URLs of the desired behavior I'm trying to achieve:
http://www.website.com/contenttoberemoved/
-- redirect to http://www.website.com/other/section/
http://www.website.com/contenttoberemoved/an-article/
-- redirect to http://www.website.com/other/section/
http://www.website.com/wp-content/themes/theme-name/custom/contenttoberemoved/images/image-name.jpg
-- perform NO redirect
And here is the current Nginx configuration:
# Addresses URL #1 above
if ( $request_filename ~ contenttoberemoved/ ) {
rewrite ^(.*) http://www.website.com/category/articles/ permanent;
}
# Addresses thousands of URLs with the structure of #2 above
if ( $request_filename ~ contenttoberemoved/.+ ) {
rewrite ^(.*) http://www.website.com/category/articles/ permanent;
}
# No idea what to do to address #3
Here is the full server block:
server {
server_name website.com www.website.com;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log error;
root /usr/share/nginx/www;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_pass 127.0.0.1:9000;
}
# redirects
if ( $request_filename ~ contenttoberemoved/ ) {
rewrite ^(.*) http://www.website.com/category/articles/ permanent;
}
if ( $request_filename ~ contenttoberemoved/.+ ) {
rewrite ^(.*) http://www.website.com/category/articles/ permanent;
}
#many page-level redirects below....
}
How can I make the redirect instructions broad enough to encompass example URLs #1 & #2 above, but specific enough to not include images (or really anything other than the posts I'm trying to target)?
Thanks!
For your URLs there is no need to use if and regexps. Simple location will be enough:
location ^~ /contenttoberemoved/ {
return 301 /category/articles/;
}
That's all.

How to combine rewrites with different roots in location blocks

I'm struggling to understand how to control rewrites to PHP scripts when the location blocks that do the rewrites require different roots.
Here's the simplified example. My catch-all front controller must be in the web root, but the firewall.php script must not be.
The purpose of this is to provide gated access to download files not under the root.
server {
# Requests for media forced through firewall script outside web root
# /path/to/secure-area/firewall.php
#
location ^~ /downloads/ {
root /path/to/secure-area;
rewrite ^/.+$ /firewall.php last;
}
# Regular requests bootstrap front controller under web-root
# /path/to/web-root/index.php;
#
location / {
root /path/to/web-root;
index index.php
if ( -e $request_filename ) {
break;
}
rewrite ^.+$ /index.php last;
}
# Execute PHP via php-fpm
# rewrites hitting this are not mapping request_filename properly
#
location ~ \.php$ {
if ( !-f $request_filename ) {
return 404;
}
include /usr/local/nginx/conf/fastcgi_params;
fastcgi_pass unix:/tmp/php.sock;
fastcgi_index index.php;
}
}
The obvious solution is having a common root, but this is not possible in my situation. I cannot place the secure location under the web root and the web root must stay as-is.
It looks as if the root directive is only effective within the location block that defines it. The rewrites are individually working fine, but when the ~ \.php block is hit, the root is lost.
I'm obviously doing it wrong, so how should I achieve this?
Untested, but something like this ought to help you. It uses the http://wiki.nginx.org/XSendfile to serve the protected content from a different root. Also uses try_files which is a much better pattern for Front Controllers.
server {
# More here: http://wiki.nginx.org/XSendfile
#
# To serve /downloads/some.zip
# get php to set the http header:
#
# X-Accel-Redirect: /downloads/some.zip
#
# and then the file /path/to/secure-area/downloads/some.zip
# will be sent by nginx
location /downloads/ {
internal;
root /path/to/secure-area;
}
location / {
root /path/to/web-root;
index index.php
try_files $uri $uri/ /index.php;
}
# make sure you read http://wiki.nginx.org/Pitfalls
location ~* \.php$ {
try_files $uri =404;
fastcgi_pass unix:/tmp/php.sock;
fastcgi_index index.php;
include /usr/local/nginx/conf/fastcgi_params;
}
}

Resources