31

I want any requests like http://example.com/whatever/index.php, to do a 301 redirect to http://example.com/whatever/.

I tried adding:

rewrite ^(.*/)index.php$ $1 permanent;

location / {
    index  index.php;
}

The problem here, this rewrite gets run on the root url, which causes a infinite redirect loop.

Edit:

I need a general solution

http://example.com/ should serve the file webroot/index.php

http://example.com/index.php, should 301 redirect to http://example.com/

http://example.com/a/index.php should 301 redirect to http://example.com/a/

http://example.com/a/ should serve the index.php script at webroot/a/index.php

Basically, I never want to show "index.php" in the address bar. I have old backlinks that I need to redirect to the canonical url.

2
  • well a redirect loop is def a logical thing to happen, because both URL's are the same, the whatever/ will call index.php because you most probably have an index index.php line above Commented Feb 10, 2014 at 22:02
  • I want to 301 redirect the external url, but internally serve the file index.php. How do I do that?
    – jcampbell1
    Commented Feb 11, 2014 at 0:26

4 Answers 4

75
+50

Great question, with the solution similar to another one I've answered on ServerFault recently, although it's much simpler here, and you know exactly what you need.

What you want here is to only perform the redirect when the user explicitly requests /index.php, but never redirect any of the internal requests that end up being served by the actual index.php script, as defined through the index directive.

This should do just that, avoiding the loops:

server {
    index index.php;

    if ($request_uri ~* "^(.*/)index\.php$") {
        return 301 $1;
    }

    location / {

        # ...
    }
}
17
  • 5
    Great answer. I wonder how it could be done without the if() {} syntax and instead as a one liner? Anyway, for now, I modified it a bit to preserve the query string if any: if ($request_uri ~* "^(.*/)index\.php(?:.*)$") { return 301 $1$is_args$args; }
    – J W
    Commented May 17, 2014 at 20:25
  • 5
    I think passing the query parameters should be allowed in it. Changing it to: if ($request_uri ~* "^(.*/)index\.php(.*)") { return 301 $1$2; }
    – Neo
    Commented Sep 8, 2014 at 12:46
  • 5
    Nice one @Neo To avoid getting double slash // I changed a little # Strip index.php to avoid duplicate content if ($request_uri ~* "^(.*/)index\.php(/?)(.*)") { return 301 $1$3; }
    – Jerem
    Commented Jan 21, 2016 at 16:50
  • 1
    @Jacob Ford How do I need to modify the expression to match index.html and index without html (as extension)?
    – user10202925
    Commented Dec 18, 2019 at 16:31
  • 1
    @AndrewK — if ($request_uri ~* "^(.*/)index(\.(php|html))?/?$") { return 301 $1; }
    – cnst
    Commented Dec 18, 2019 at 20:10
2

Try that

location ~ /*/index.php {
    rewrite ^/(.*)/(.*) http://www.votre_domaine.com/$1 permanent;
}
location /index.php {
    return 301 http://www.example.com/;
}
2

If you already have first line mentioned below in your Nginx configuration file you don't have rewrite it again.

index index.php index.html index.htm;

rewrite ^(/.).html(?.)?$ $1$2 permanent;

rewrite ^/(.*)/$ /$1 permanent;

try_files $uri/index.html $uri.html $uri/ $uri =404;

This will remove .html from the URL and additionally will also remove "index" from home page or index page. For example - https://www.example.com/index will be changed to https://www.example.com

-3

Try

location = /whatever/index.php {
    return 301 $scheme://www.example.com/whatever/;
}

Another benefit from doing it this way is that nginx does a return faster than a rewrite.

1
  • 1
    I want something that works generally: /a/index.php -301-> /a/, /b/index.php -301-> /b/
    – jcampbell1
    Commented Feb 10, 2014 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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