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.

Let's say I have an index.php file inside my public directory.

Using DirectoryIndex index.php, I can set that file as my default index in my website.

But what if I don't want users to enter through www.example.com/index.php, only by www.example.com, how can I do that?

EDIT

I added the following code:

if (strpos($_SERVER['REQUEST_URI'], 'index.php')) {
    readfile('404.html');
    exit;
}

at the beginning of index.php, so that entering by www.example.com/index.php would result in a 404 error but entering by www.example.com would not (thus, giving no information to a potential attacker of whether I use PHP or not). Not the best solution in the world but does work.

share|improve this question

4 Answers 4

Your question makes no sense. If you want to display a web page to the user who requests www.example.com then there has to be a DirectoryIndex of some sort.

DirectoryIndex index.php index.html 

will cause apache to search the DocumentRoot of the server for index.php and then index.html and present the rendered version of the first one found to the user.

share|improve this answer
    
Yeah, I messed up, I meant DirectoryIndex, not "DefaultIndex". But how can I make users enter only through www.example.com and not www.example.com/index.php if that's even possible? –  fedeetz Dec 10 '11 at 23:42

You can't really "hide" the page. You can deny access (Resulting in a 403 error) using htaccess. You can also redirect using mod_rewrite, either from /index.php (as suggested) to / or from /index.php to your 404 file (as you are doing now). The closest thing to hiding the page would be to use mod_rewrite to go from index.php to /, so when users enter either www.example.com or www.example.com/index.php, it shows up as www.example.com

share|improve this answer

You could 'forbid' /index.php with something like

<Location /index.php>
   Order deny,allow
   Deny from all
</Location>

Or redirect from /index.php to / with mod_rewrite (which is a bit more user friendly).

share|improve this answer
    
Thanks, but I didn't want to forbid it's access nor redirect the user, I wanted to hide it. I don't know if that's possible using .htaccess really –  fedeetz Dec 11 '11 at 20:16

I see in your comment to Koos that you want to hide index.php, which you can't really do. You best course of action is to place the parts you want hidden in another file and include that file in index.php. Done right the user will not see the include statement nor the included file, only the resulting output.

share|improve this answer

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.