How can I make this php redirect not be conflicted by Nginx

How can I make this php redirect not be conflicted by Nginx

nginx rewrite vs redirect
nginx too many redirects
nginx internal redirect
nginx too many redirects proxy_pass
nginx redirect http to https
nginx redirect 301
nginx forward to another server
how to redirect from one page to another in php

My php file 404.php contains a php redirect at the top of the file:

<?php
    header('Location: some-page.html');

If I go to www.mysite.com/404, it redirects to www.mysite.com/some-page.html as expected, however, if I go to www.mysite.com/non-existent-page, the redirect does not work.

In my nginx.conf:

location ~* \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

error_page 404 =200 /404.php;

try_files $uri @remExt;
location @remExt {
    rewrite ^(.*)$ $1.php last;
}
if ($request_uri ~ ^/([^?]*)\.php($|\?)) {
    return 301 /$1$is_args$args;
}
rewrite ^/index$ / permanent;
rewrite ^/(.*)/$ /$1 permanent;

How can I make the php redirect work when visiting www.mysite.com/non-existent-page?

PS

Sorry for the badly written question title. The clear and better written question title I wanted to use was automatically rejected.


Try this in your nginx config and restart it.

error_page 404 /404.php;
location = /404.php {
    root /path/to/www/;
    internal;
}

error too many redirect on nginx ?, My site have just install SSL but SSL not work. a error too many redirect I run command nginx -t and receive a warning nginx: [warn] confli. nginx: [warn] conflicting server name “latena.vn” on 0.0.0.0:80, ignored index index.php index.html index.htm; How do we use a GUI to manage the database? Solution at a Glance. In Nginx, you can accomplish most redirects with the built-in rewrite directive. This directive is available by default on a fresh Nginx installation and can be used to create both temporary and permanent redirects. In its simplest form, it takes at least two arguments: the old URL and the new URL.


/404.php

<?php
    http_response_code(301);
    header('Location: /the/page/you/want');

nginx.conf

error_page 404 /404.php
location / {
    try_files $uri $uri.php $uri/ =404;
}

then check syntax with nginx -t and reload nginx with systemctl reload nginx(if you're using systemd)

http://php.net/manual/en/function.http-response-code.php

Nginx - Conflicting server name issue & redirection loop, You can use this config to redirect all the http requests to https and move all the non-www requests. I use this on my server configurations and they are working  To make matters worse, too many 404s will have an impact on your search engine site rankings. We can battle 404s with redirect rules written in our Nginx configurations. The rules can either suggest to the web browser, search engine and anything that the content has permanently moved, or we suggest the content has only moved temporarily.


It appears I have solved the problem, although there may be some edge cases where my Nginx code fails.

The problem was with this line error_page 404 =200 /404.php;. The solution is to use try_files inside location ~* \.php$.

index index.php;

location ~* \.php$ {
    try_files $uri $uri/ /404.php$is_args$args;
    fastcgi_pass unix:/var/run/php-fpm/php-fpm-root.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

location / {
    try_files $uri $uri/ $uri.php$is_args$query_string;
}

if ($request_uri ~ ^/([^?]*)\.php($|\?)) {
    return 301 /$1$is_args$args;
}
rewrite ^/index$ / permanent;
rewrite ^/(.*)/$ /$1 permanent;

PHP Redirect To Another URL / Web Page Script Example, How can I use a PHP script to redirect a user from the url they entered to a Make sure that code below does not get executed when we redirect. 408 => "​HTTP/1.1 408 Request Time-out", 409 => "HTTP/1.1 409 Conflict", 410 HTTP/​1.1 301 Moved Permanently Server: nginx Date: Mon, 30 Dec 2013  Restarting nginx (via systemctl): nginx.serviceJob for nginx.service failed. See 'systemctl status nginx.service' and 'journalctl -xn' for details. systemctl status nginx.service` shows an error, but journalctl -xn doesn't show anything. failed!`. Your second solution does not cause an error, but it still doesn't redirect.


Nginx Redirect Cheat Sheet, Check out our Nginx tutorials for redirecting a site to HTTPS, www, non-www, another page or As this is an Nginx server, any .htaccess rules will not apply. Or you can create a new file name html.conf or domain.com.conf. H ow do I redirect with PHP script? How can I use a PHP script to redirect a user from the url they entered to a different web page/url? Under PHP you need to use header() to send a raw HTTP header. Using headers() method, you can easily transferred to the new page without having to click a link to continue. This is also useful for search engines.


Troubleshooting: Too Many Redirects, These conflicting redirects will flip flop back and forth and never complete. The .htaccess file is a configuration file used to modify Apache server These are not the only ways to do these kinds of redirects, but these should  If you make use of NGINX with PHP, you cannot hide the PHP information via the NGINX configuration file. Instead, you have to edit the php.ini file. Issue the command sudo nano /etc/php/7.0/fpm


header - Manual, For example, if you have configured Apache to use a PHP script to handle requests for Make sure that code below does not get executed when we redirect. */ Laravel nginx config to redirect all requests to https and an exception URL that can still be accessible via http - laravel-nginx-config-make-http-exception-url-and-make-all-others-https.md