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.

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

http://www.example.com/index.php
http://www.example.com/foo/index.php

it'll be rewritten in their address bar as

http://www.example.com/
http://www.example.com/foo/

etc

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
add comment

1 Answer 1

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);
    exit();
}

simple..?

share|improve this answer
add comment

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.