Nginx rewrite gives 404 - nginx

On a server with Ubuntu 20.04 and nginx 1.18.0 using ISPConfig 3.2 I have the following configuration for my Wordpress website:
location /api {
rewrite ^(.*)$ $1.php last;
}
location ~* ^/index\.php$ {
break;
}
try_files $uri $uri/ /index.php$is_args$args;
location /user {
try_files $uri $uri/ /index.php$is_args$args;
rewrite ^/user/([^/]+)/([^/]+)/master?$ /index.php/master/?userid=$2 redirect;
rewrite ^/user/([^/]+)/([^/]+)/chief?$ /index.php/chief/?userid=$2 redirect;
rewrite ^/user/([^/]+)/([^/]+)?$ /user/?userid=$2 redirect;
}
The redirect for e.g. /user/john/123 to /user/?userid=123 is working as expected.
But I do not want a redirect but to keep the URL as it is and show the content of the internally rewritten URL. So I tried to change redirect to break or last but both changes are resulting in a 404.
...
location /user {
try_files $uri $uri/ /index.php$is_args$args;
rewrite ^/user/([^/]+)/([^/]+)/master?$ /index.php/master/?userid=$2 last;
rewrite ^/user/([^/]+)/([^/]+)/chief?$ /index.php/chief/?userid=$2 last;
rewrite ^/user/([^/]+)/([^/]+)?$ /user/?userid=$2 last;
}
Why is it working with the redirect but not without?
Edit
Another try:
location /api {
rewrite ^(.*)$ $1.php last;
}
location ~* ^/index\.php$ {
break;
}
try_files $uri $uri/ /index.php$is_args$args;
location /user {
try_files $uri $uri/ #userrules;
}
location #userrules {
rewrite ^/user/([^/]+)/([^/]+)/master?$ /index.php/master/?userid=$2 redirect;
rewrite ^/user/([^/]+)/([^/]+)/chief?$ /index.php/chief/?userid=$2 redirect;
rewrite ^/user/([^/]+)/([^/]+)?$ /index.php/user/?userid=$2 redirect;
rewrite ^/(.*)$ /index.php?$args last;
}
Same result - if I remove the redirect it results in a 404.
The resulting page e.g. /user/?userid=203 works but I would like to keep the readable URLs.

Related

Nginx location regex not working for sub pages

I have two react applications, one at '/home/user/www' and one at '/home/user/builds/checkout'. I want any url starting with '/checkout' eg. '/checkout/complete', '/checkout/error' to use that application. I have the below setup in my Nginx config file:
root /home/user/www;
index index.html index.htm;
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /index.html break;
}
}
location ~ ^/checkout?(.*)$ {
allow all;
root /home/user/builds;
if (!-e $request_filename){
rewrite ^(.*)$ /index.html break;
}
try_files $uri $uri/index.html =404;
}
location ~ ^.+\..+$ {
try_files $uri =404;
}
going to the url '/checkout' is working correctly but any other url begining with '/checkout' like '/checkout/complete' and 'checkout/error' are just returning a 404 page.
Your configuration looks too complicated. Can you try this one?
root /home/user/www;
index index.html index.htm;
location / {
try_files $uri /index.html;
}
location /checkout/ {
root /home/user/builds;
try_files $uri $uri/ /checkout/index.html;
}
If the /checkout URI won't redirect you to /checkout/, add this:
location = /checkout {
return 301 /checkout/;
}

nginx url rewrite getting failed

I am trying to migrate my .htaccess rules to nginx. I have tried almost all the questions on SO & url rewriter as well but not getting success. In short i want to convert following dynamic urls:
from
[1] - https://vc.test/results.php?url=ngo-service
[2] - https://vc.test/jobs.php?d=17&t=oil-&-gas
[3] - https://vc.test/jobs.php?d=17
To
[1] - https://vc.test/ngo-service
[2] - https://vc.test/17/oil-&-gas
[3] - https://vc.test/17
Request help to sort out this issue.
My nginx effort
server {
listen 127.0.0.1:80;
listen 127.0.0.1:443 ssl http2;
ssl_certificate_key "d:/winnmp/conf/opensslCA/selfsigned/vc.test+4-key.pem";
ssl_certificate "d:/winnmp/conf/opensslCA/selfsigned/vc.test+4.pem";
server_name vc.test;
root "d:/winnmp/www/vc";
## Access Restrictions
allow 127.0.0.1;
deny all;
autoindex on;
location / {
index index.html index.htm index.php;
try_files $uri $uri.html $uri/ #extensionless-php;
if ($query_string ~* "fbclid="){
rewrite ^(.*)$ /$1? redirect;
break;
}
if ($query_string ~* "url="){
rewrite ^(.*)$ /%1? redirect;
rewrite ^/(.*)$ /results.php?url=$1 permanent;
break;
}
rewrite ^/([0-9]+)/(.*)?$ jobs.php?d=$1&t=$2 break;
rewrite ^/([0-9]+)?$ jobs.php?d=$1 break;
}
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
location ~ \.php$ {
try_files $uri =404;
include nginx.fastcgi.conf;
include nginx.redis.conf;
fastcgi_pass php_farm;
fastcgi_hide_header X-Powered-By;
}
}
I don't know what your if ($query_string blocks are for, so I will ignore them.
Use rewrite...last if the rewritten URI is to be processed in a different location block, for example with URIs ending with .php. All Nginx URIs begin with a leading /, for example, use /jobs.php and not jobs.php.
You can place your list of rewrite statements in the location / block, and they will be evaluated in order until a match is found. If no match is found, the try_files statement will be evaluated. That's just how the rewrite module works!!
However, the 1st rewrite rule is too general and may break some of the URIs intended to be fulfilled by the try_files statement. A better solution may be to put all of the rewrite statements into the same named location block.
For example:
index index.html index.htm index.php;
location / {
try_files $uri $uri.html $uri/ #rewrite;
}
location #rewrite {
if (-f $document_root$uri.php) {
rewrite ^ $uri.php last;
}
rewrite ^/([0-9]+)/(.+)$ /jobs.php?d=$1&t=$2 last;
rewrite ^/([0-9]+)$ /jobs.php?d=$1 last;
rewrite ^/([^/]+)$ /results.php?url=$1 last;
return 404;
}
location ~ \.php$ {
try_files $uri =404;
...
}
See this caution on the use of if.

Nginx - remove trailing slash if it's not a directory

I have no problems with redirect, but when I uncomment 2 lines that do "if folder doesn't exist" check, I get 404 error on any page. And I don't understand why, it's basic functionality.
I need to redirect all requests with trailing slash to urls without it. The only exception is when a folder with that url exists.
location / {
#if (!-d $request_filename) {
rewrite ^/(.*)/$ /$1 permanent;
#}
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?q=$request_uri;
}
You should read this caution on the use of if.
You could try an alternative approach and only rewrite after try_files has processed the URIs that it can:
location {
index index.php index.html index.htm;
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^/(.*)/$ /$1 permanent;
rewrite ^ /index.php?q=$request_uri last;
}
See this document for more.

Jump #name gives me 404 error on nginx

When i click on a button using #jumpto it gives me a 404 error. I think it has something to do with the nginx config file because i am working on it.
How can I use #jump without giving me 404 error?
server {
listen 80;
server_name doutor.pt;
return 301 http://www.doutor.pt$request_uri;
}
server {
server_name doutor.pt www.doutor.pt;
access_log /var/log/nginx/doutor.pt.access.log;
error_log /var/log/nginx/doutor.pt.error.log;
root /var/www/doutor.pt/htdocs;
index index.php index.html;
location / {
try_files $uri $uri/ #rewrite;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
}
location #rewrite {
rewrite ^/sitemap-doctors-([0-9]+).xml$ /index.php?page_name=sitemap&category=doctors&page=$1 las$
rewrite ^/sitemap-doctors.xml$ /index.php?page_name=sitemap&category=doctors&page=0 last;
rewrite ^/sitemap.xml$ /index.php?page_name=sitemap last;
rewrite ^/category/([0-9]+)-([^/]+)$ /index.php?page_name=doctors&category=$1 last;
rewrite ^/medico/([^/]+)$ /index.php?page_name=medico&doctor_url=$1 last;
rewrite ^/([^/]+)/([^/]+)$ /index.php?page_name=$2&page_category=$2 last;
rewrite ^/([^/]+)$ /index.php?page_name=$1 last;
# some default action???
return 404;
}

Pretty URL/Clean URL + nginx

I'm trying to get example.com/language/en to point to example.com/language?lang=en I've already got the #language to work (kind of, there's still some bug that I explain in the end) but unfortunately I don't know how to make the #language and the #extensionless-php to work at the same time, I tested the #language code by removing the #extensionless-php in the location / and adding the #location.
And the way it is right now it accepts more than one parameter like this //example.com/language/en/en/en and I want it to return a 404 error if it has more than 1 / after the language
Current config:
location / {
try_files $uri $uri.html $uri/ #extensionless-php;
}
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
location #language {
rewrite ^/language/(.*)$ /language.php?lang=$1 last;
}
The answer was simply removing rewrite ^/language/(.*)$ /language.php?lang=$1 last; from the #language, edited the regular expression so that when the URI is /language/en/ it goes to a 404 error
location / {
try_files $uri $uri.html $uri/ #extensionless-php;
}
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
rewrite ^/language/([^/]+)$ /language.php?lang=$1 last;

Resources