Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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:

site.com/?page=show.php&id=2748

site.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.site.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.site.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.site.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;

share|improve this question

migrated from stackoverflow.com Sep 10 '11 at 23:36

This question came from our site for professional and enthusiast programmers.

2 Answers 2

up vote 2 down vote accepted

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 ;-)

share|improve this answer
    
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

UPDATED:

Add the following along with your existing condition:

if ($args ~* "php") {
    rewrite ^/(.*) http://www.site.com permanent;
}
share|improve this answer
    
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. –  RakeshS Aug 30 '11 at 3:38
    
the same problem happened. I tried put a "break" but it doesnt work as well.... –  Thomas Aug 30 '11 at 19:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.