Rewrite in Subpath 404 urls with Nginx - 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.

Related

Nginx Wordpress Internal rewrite WITHOUT changing URL/URI

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?

Show custom page for not found url

I want to show certain pages for my url
For example when visiting below link
https://www.example.com/blog/unlock-subconscious-mind-power/
must show content of rewrite/blog.php
But I am getting not found URL.
I tried below code
fastcgi_read_timeout 3200;
fastcgi_intercept_errors on;
location /blog {
try_files $uri $uri/ $uri/index.php #rewrites;
}
location #rewrites {
rewrite ^([^.]*[^/])$ $1/ permanent;
rewrite ^ /rewrite/blog.php last;
}
Its still giving 404 error.

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; }

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.

Nginx rewrite rule results in error 403

I am trying to rewrite a url like http://example.com/extras/?cat=help&page=faq to something along the lines of http://example.com/extras/help/faq.
Well, rewrite as in a link or a user types the latter and the server understands that it's supposed to be the former. However, after implementing the rewrite that is ostensibly supposed to do what I want, navigating to pages results in a 403 code, because the server is taking what's in the URL bar literally. Because I have the server set up to disallow direct access to the subfolders, the 403 code is returned.
Below is the php code on my site that handles loading the pages to be included within the /extras index.php:
if(!empty($_GET['cat']) && !empty($_GET['page'])) {
$folder = $_GET['cat'];
$page = $_GET['page'] . '.php';
$pages = scandir($folder);
unset($pages[0], $pages[1]);
$url .= $folder . DIRECTORY_SEPARATOR;
if(file_exists($url . $page) && in_array($page, $pages)) {
$url .= $page;
include($url);
} else {
//Invalid category or page given
header("HTTP/1.0 404 Not Found");
}
} else {
//No category or page given; fall back to contents
include("contents.php");
}
The goal of the above file is for the contents of the subfile in the subfolders to be included in the body of index.php, not for the browser to actually try and navigate to the subfile.
This is (part of) the nginx config:
server {
listen 80 default_server;
listen [::]:80 ipv6only=on;
root /usr/share/nginx/html;
server_name localhost;
error_page 403 /;
error_page 404 /error/404.php;
error_page 500 502 503 504 /error/50X.php;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ #no-extension;
allow 192.168.0.0/24;
allow 127.0.0.1;
deny all;
}
location /help {
try_files $uri $uri/ #help #no-extension;
}
location #help {
rewrite "^/help/([^/]*)/([^/]*)$" /help/?cat=$1&page=$2 last;
}
location ~ /help/(help|otherfolder|morefolders) {
deny all;
}
# PHP Handler
location ~ \.php$ {
try_files $uri $uri/ =404;
include fastcgi_params;
fastcgi_pass php5-fpm-sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_intercept_errors on;
}
location #no-extension {
rewrite ^(.*)$ $1.php last;
}
}
Any way to make the rewrite take effect instead of the browser trying to access a file it will never be able to?
while not exactly answering your question, would it not be simpler to
location /help {
try_files $uri $uri/ /index.php;
when you load /help/faq/ its passed in REQUEST_URI to /index.php which does the subsequent regex split and file include...no need to rewrite. specific location blocks you want to restrict should be place above, so the deny will match first (if i recall correctly)

Resources