Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Simple question really, as in the title.

I did some googling but all I seemed to find was advice on how to rewrite every page to index.php?theurigiven, etc.

I just want a rule that will make it so that if someone accidentally goes to

it'll be rewritten in their address bar as


I tried a few simple regexps but they didn't seem to be working, so I slimmed it down to the bare-bones of

location /index.php {
    return 301 /;

And even that doesn't seem to work?

I'm guessing it's because internally nginx is silently re-routing "/" to "/index.php" (or whatever I've specified in my "index" directive, so that those files can be passed over to my php handler, via the "if my page ends in .php and the file exists, send it to PHP" block...?

share|improve this question
You know that index.html is not index.php right? – Michael Hampton Oct 13 '13 at 15:51
I do, sorry. I wrote "index.php" as that's what most people use as their index page, but I'm naievely using index.html in a vain attempt to "cloak" php from my users. So I just got mixed up in my question, I'll edit it now. – Codemonkey Oct 13 '13 at 16:01
up vote 0 down vote accepted

So after much faffing, I think I've come to the conclusion that this isn't nicely doable in nginx directives. It is doable, but only by using if statements, which everything says to avoid if possible.

So I might go down a PHP route instead:

if (substr($_SERVER["REQUEST_URI"],-9)=='index.php') {
    Header ('Location: '.substr($_SERVER["REQUEST_URI"],0,-9),true,301);


share|improve this answer

Your Answer


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.