2

Recently I changed my website that was used PHP/Apache to Django/Nginx. I would like to give a http response 301 and redirect to my main page all requests that contain ".php" in url.

Example:

example.com/**?page=show.php&id=2748**

example.com/**index.php?page=show_page.php&id=2748**

The ".php" can be in any position in the URL.

I tried:

location ~ \.php {
        rewrite ^/(.*) http://www.example.com permanent;
    }   

But, of course, it just work when have .php in the end. Can anybody give me a clue?

UPDATED: If I use this solution:

            if ($request_uri ~* "php") {
           rewrite ^/(.*) http://www.example.com permanent;
        }   

I get the following error in the log:

[29/Aug/2011:13:30:25 -0300] "GET /?page=show_page.php&id=2748 HTTP/1.0" 301 184 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0"
[29/Aug/2011:13:30:26 -0300] "GET /?page=show_page.php&id=2748 HTTP/1.0" 301 184 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0"
[29/Aug/2011:13:30:26 -0300] "GET /?page=show_page.php&id=2748 HTTP/1.0" 301 184 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0"
[29/Aug/2011:13:30:27 -0300] "GET /?page=show_page.php&id=2748 HTTP/1.0" 301 184 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0"
[29/Aug/2011:13:30:27 -0300] "GET /?page=show_page.php&id=2748 HTTP/1.0" 301 184 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0"
[29/Aug/2011:13:30:28 -0300] "GET /?page=show_page.php&id=2748 HTTP/1.0" 301 184 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0"
[29/Aug/2011:13:30:28 -0300] "GET /?page=show_page.php&id=2748 HTTP/1.0" 301 184 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0"

Why it tries to redirect too many times?

UPDATED:

I Tried:

  if ($request_uri ~ .*.php.*) {
        rewrite ^/ http://www.example.com permanent;
    }

its working, but when the URL starts with "?" I get same error. So, 50% of the problem was solved...

SOLUTION:

        if ($request_uri ~ .*.php.*) {
            #return 410;
        rewrite ^ http://$host? permanent;
Aditya Aggarwal
  • 123
  • 1
  • 5
Thomas
  • 218
  • 1
  • 2
  • 8

2 Answers2

7

The problem was in the rewrite.

I changed rewrite to return code 410, I think that I would do that instead return return 301 (permanent).

So, here is the code:

if ($request_uri ~ .*.php.*) {
        return 410;
}

Now, nginx will return 4010 for all pages that that contain ".php" in anywhere in URL. My django site can live in peace now ;-)

Thomas
  • 218
  • 1
  • 2
  • 8
  • Well, after some days I realized that I really needed permanent redirect. Here is the correct solution: if ($request_uri ~ .*.php.*) { #return 410; rewrite ^ http://$host? permanent; – Thomas Sep 21 '11 at 17:37
0

UPDATED:

Add the following along with your existing condition:

if ($args ~* "php") {
    rewrite ^/(.*) http://www.site.com permanent;
}
Rakesh Sankar
  • 262
  • 4
  • 12
  • 1
    Hello RakeshS, it doesnt work... If I check the log I will get: [29/Aug/2011:13:30:25 -0300] "GET /?page=show_page.php&id=2748 HTTP/1.0" 301 184 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0" [29/Aug/2011:13:30:26 -0300] "GET /?page=show_page.php&id=2748 HTTP/1.0" 301 184 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0" [29/Aug/2011:13:30:26 -0300] "GET /?page=show_page.php&id=2748 HTTP/1.0" 301 184 "-" (the same message are repeated for a couple of times and a error show in my browser) – Thomas Aug 29 '11 at 16:31
  • @Thomas-bryan I have posted the updated settings, please give it a try. – Rakesh Sankar Aug 30 '11 at 03:38
  • the same problem happened. I tried put a "break" but it doesnt work as well.... – Thomas Aug 30 '11 at 19:56