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.
Related
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.
This is working
https://example.com/ifsc-code/index.php?bank=axis-bank&state=west-bengal&district=kolkata&branch=bally
This is showing error404
https://example.com/ifsc-code/axis-bank/west-bengal/kolkata/bally
I am using this code in Nginx.conf file
location /ifsc-code {
try_files $uri $uri/ /index.php?$args;
rewrite ^/(.*)/(.*)/(.*)/(.*)/?$ /index.php?bank=$1&state=$2&district=$3&branch=$4;
rewrite ^/(.*)/(.*)/(.*)/?$ /index.php?bank=$1&state=$2&district=$3;
rewrite ^/(.*)/(.*)/?$ /index.php?bank=$1&state=$2;
rewrite ^/(.*)/?$ /index.php?bank=$1;
}
I changed the code and my problem has solved, hope it will help someone.
location /ifsc-code {
root /var/www/example/ifsc-code;
index index.php index.html index.htm;
try_files $uri $uri/ #ifsc-code;
}
location #ifsc-code {
rewrite ^/ifsc-code/(.*)/(.*)/(.*)/(.*)/?$ /ifsc-code/index.php?bank=$1&state=$2&district=$3&branch=$4;
rewrite ^/ifsc-code/(.*)/(.*)/(.*)/?$ /ifsc-code/index.php?bank=$1&state=$2&district=$3;
rewrite ^/ifsc-code/(.*)/(.*)/?$ /ifsc-code/index.php?bank=$1&state=$2;
rewrite ^/ifsc-code/(.*)/?$ /ifsc-code/index.php?bank=$1;
}
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.
I am attempting to rewrite my /support url, so I can grab the "page" as an appended querystring.
The issue I am running into now, is my assets are also contained in the /support subfolder, so they too are getting re-written.
How can I change this to exclude my assets? (where assets = /support/assets/styles, /support/assets/scripts, etc...)
Here is my current location block
location /support/ {
index index.php;
rewrite ^/support/(.*) /support/index.php?_p=$1 last;
try_files $uri $uri/ /support/index.php?$args;
}
You can check for the assets with try_files before performing the rewrite by using a named location.
For example:
location /support/ {
index index.php;
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^/support/(.*) /support/index.php?_p=$1 last;
}
See this document for more.
I'm trying to configure nginx to work with a pushState-enabled app. I've got it mostly working, but what I'm missing is getting a 404 returned on non-existent documents. How can I achieve this? Relevant part of my nginx.conf:
location / {
root /foo;
index index.html;
try_files $uri /index.html;
}
I've tried try_files $uri /index.html =404;, but that didn't work, obviously, because index.html exists.
I've also tried...
if (!-e $request_filename) {
return 404;
}
...in the server and location block. Neither worked.
Try this. If file exists, redirect to index.html. Otherwise return 404.
location = /index.html {}
location / {
if (!-f $request_filename) {
return 404;
}
rewrite ^/(.*) /index.html redirect;
}