How to redirect Nginx 400 error to website desired page?

June 4, 2015 3.7k views
Nginx PHP CentOS

I have a website for a client, but when I go to www.example.com and insert special characters, let's say www.example.com/$%#&, Nginx give me a 400 Bad Request error.

What I want here is, instead of that error, redirect the user automatically to the home page.

How can I do that?

3 Answers

nano /etc/nginx/sites-available/default

Add

error_page code_number file_source;

e.g error_page 400 /;

service nginx restart

Thank you so much.

What I'm hosting is a web app, and its index y located in /web/app.php. So I put in Nginx:

error_page 400 /web/app.php;

It works perfectly, but now when the redirection happens, I can see the url www.example.com/web/app.php. How can I do to make it look just www.example.com?

  • Add error_page 400 = @400; in :
    ```location / {
    ......
    }

    And finally add  (not in location / !!!): 
    ```location @400 {
             rewrite ^(.*)$ / permanent;
            }
    

    Remove the old

Have another answer? Share your knowledge.