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

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, only by, how can I do that?


I added the following code:

if (strpos($_SERVER['REQUEST_URI'], 'index.php')) {

at the beginning of index.php, so that entering by would result in a 404 error but entering by 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

Your question makes no sense. If you want to display a web page to the user who requests 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 and not if that's even possible? – federicot 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 or, it shows up as

share|improve this answer

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

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

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 – federicot 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


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.