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.
Related
I have a simple website with some PHP files in two languages: the main language and the translation, let's say Spanish.
I want to show a 404 error page for the main language if someone tries to load an non-existing URL, and a translated 404 error page for non-existing URLs in the /es subdirectory.
This is my config file:
server {
listen 80;
server_name example.com;
root /var/www/example.com;
location / {
error_page 404 /404.php;
try_files $uri $uri/ #extensionless-php;
index index.php;
}
location /es {
error_page 404 /es/404.php;
try_files $uri $uri/ #extensionless-php;
index index.php;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/run/php/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
}
It is not working as expected. When loading a non-existing URL (whether in the root or in the /es subdirectory) nginx returns the standard 404 page (the one displaying 404 Not Found).
If I add error_page 404 /404.php; in the server block (outside location blocks), then example.com/foo and example.com/es/foo would return the non-translated 404 error page.
How can I fix this?
Any of your requests for the non-existent resources ends up at the location ~ \.php$ { ... } block where you do not have error_page directive at all. If you define your custom error page at the server level, that location inherits that page. You can define two PHP handlers (order of location blocks matters!):
location ~ ^/es.+\.php$ {
error_page 404 /es/404.php;
... # other content is the same as in the question
}
location ~ \.php$ {
error_page 404 /404.php;
... # other content is the same as in the question
}
or better use a map block like
map $uri $errpage {
~^/es /es/404.php;
default /404.php;
}
server {
...
error_page 404 $errpage;
}
I'm trying to make url on my server seo friendly.
Original: /face.php?name=hello-stackoverflow123
Desired: /face/hello-stackoverflow123
I made those little configs:
location / {
try_files $uri $uri.html $uri/ #extensionless-php;
index index.html index.htm index.php;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
Problem 1: I can't find a config to keep the parameters it gives me always 404 (so I get 404 if I try face/hello-stackoverflow123
Problem 2: If I have a file faces.php it will open if I try/faces, but if I open faces.php it won't redirect to /faces
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.
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; }
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.