Pretty URL/Clean URL + nginx - 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;

Related

Nginx rewrite gives 404

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.

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.

How to convert url segment to querystring with Nginx rewrite

I whould like this simple rewrite rule:
http://example.com/8743b52063cd84097a65d1633f5c74f5?param1=999&param2=2222
to be redirected to:
http://example.com/index.php?param1=999&param2=2222&hash=8743b52063cd84097a65d1633f5c74f5
The following is my default location:
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
How can I achieve this using Nginx rewrite?
Using a rewrite statement:
rewrite "^/(\w{32})$" /index.php?hash=$1 last;
Or, within a location block:
location ~ "^/(?<hash>\w{32})$" {
rewrite ^ /index.php?hash=$hash last;
}

Nginx rewrite rules, multiple pages

Hello i am having some issues writing my nginx rewrite rules.
Here is what i have so far:
location / {
rewrite ^/handel/(.*)/(.*)/(.*)/(.*)$ /test.php?cat=$1&underCat=$2&underUndercat=$3&name=$4 last;
if (!-e $request_filename){
rewrite ^(.*)$ $1.php last;
}
try_files $uri $uri/ /index.php?$query_string = 404;
}
What i wish to do is:
/handel/(.*)/(.*)/(.*)/(.*)$ -> /page1.php?cat=$1&underCat=$2&underUnderCat=$3&name=$4
/handel/(.*)/(.*)/(.*)$ -> /page2.php?cat=$1&underCat=$2&underUnderCat=$3
/handel/(.*)/(.*)$ -> /page3.php?cat=$1&underCat=$2
/handel/(.*)$ -> /page4.php?cat=$1
/handel/ -> /page5.php
Please note the diffrent pages.

Resources