Redirect any files in a folder to a certain page

Let me explain. I want only www.codefundamentals.com/admin/index.php to be accessed (preferably by me. I will have a login page from there in case people DO find it) but anyway I want any and all other pages like “admin/randompage.php” to be redirected to admin/index.php. Is there some sort of code for this?

Finding the key words to search was difficult so I do apologize if this has been asked a million times before. I did attempt a search.

My hosting allows for redirects via cpanel so I tried creating one and if I go to, for exmaple, codefundamentals.com/admin/random.php, it then goes to codefundamentals.com/admin/index.phpindex.php(etc etc etc) creating an infinite loop and thus timing out.

This is my .htaccess where I see it has actually put the cpanel redirect in there

RewriteEngine onRewriteCond %{HTTP_HOST} ^codefundamentals\\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\\.codefundamentals\\.com$
RewriteRule ^admin\\/$ "http\\:\\/\\/www\\.codefundamentals\\.com\\/admin\\/index\\.php" [R=301,L]




# Use PHP5.4 as default
AddHandler application/x-httpd-php54 .php
RewriteCond %{HTTP_HOST} ^codefundamentals.com [NC]
RewriteRule ^(.*)$ http://www.codefundamentals.com/$1 [L,R=301]

I guess while I’m at it I’ll need a way for .htaccess to know NOT to redirect to index.php unless I’m logged in already at index.php

Update, I got a validation .htaccess / .htpasswd set up, so I just need the redirect now which checks if I’m logged in, and if I’m not, any attempts at going at my admin folder will go to admin/index.php where it asks for validation (also I did not realize that .htaccess only affects the directory it is placed in (and subdirectories accordingly)

Also my work computer won’t ask for validation - it only goes straight to 401 forbidden. Anyone know why this is? I did reload trying a lot when I was beta testing it. Did I run out of attempts or something?

Try this script which is used by CodeIgniter to redirect everything to the index.php

https://ellislab.com/codeigniter/user-guide/general/urls.html

May I also suggest that now is a good time to learn a PHP Framework.

Don’t get mad but I’m too noob to see how this codeigniter would be of use to me. I’ve read through the code and it doesn’t seem to help my situation (if it does, please just understand I’m completely new to this side of things.)

This code DOES seem to redirect to my index.php although I only want that happening if my access username/password in my .htaccess hasn’t been given. Thanks for helping John. I’m going to test it out now so perhaps I’ll be eating my words :).

[FONT=Courier New]RewriteEngine on
 RewriteCond $1 !^(index\\.php|images|robots\\.txt)
 RewriteRule ^(.*)$ /index.php/$1 [L][/FONT]

No problem, my knowledge of htaccess is severely limited and I prefer using PHP.

If the /admin/.htaccess redirects correctly then put the following script at the top of your index.php

/admin/index.php


<?php 
    session_start();

    // hardcoded test 
        $msg = 'Sorry Sunshine you are not allowed anywhere near here.';
        $_SESSION['uname'] = 'Ryan';     // GOOD
        // REM the following line to test for uname = 'Ryan'
        $_SESSION['uname'] = $msg ;    
        $_SESSION['pword'] = '123456';

    // set defaults if and only if NOT set 
        $_SESSION['uname'] = isset($_SESSION['uname']) ? $_SESSION['uname'] : $msg;
        $_SESSION['pword'] = isset($_SESSION['pword']) ? $_SESSION['pword'] : '123456' ;

  if( $_SESSION['uname'] === 'Ryan' && $_SESSION['pword'] === '123456' ):
      header('Location: /admin/admin.php');
      exit;
  endif;    
    echo '<br />' .$_SESSION['uname'];
    die;

It appears that the redirect script is redirecting to my codefundamentals.com/index.php (and seemingly confirmed based off my source code). Yet the URL stays at codefundamentals.com/admin/admin.php.

Whoops, instead of redirecting (using the header(‘Location: …’); just let if fall-through because the login details are correct.



 <?php 
    session_start();

    // hardcoded test 
        $msg = 'Sorry Sunshine you are not allowed anywhere near here.';
        $_SESSION['uname'] = 'Ryan';     // GOOD
        // REM the following line to test for uname = 'Ryan'
        $_SESSION['uname'] = $msg ;    
        $_SESSION['pword'] = '123456';

    // set defaults if and only if NOT set 
        $_SESSION['uname'] = isset($_SESSION['uname']) ? $_SESSION['uname'] : $msg;
        $_SESSION['pword'] = isset($_SESSION['pword']) ? $_SESSION['pword'] : '123456' ;

  if( $_SESSION['uname'] === 'Ryan' && $_SESSION['pword'] === '123456' ):
      // header('Location: /admin/admin.php');
      // exit;
  else;    
    echo '<br />' .$_SESSION['uname'];
    die;  
   endif;

   // Only reaches here if login is valid
   // your admin code


John, I was talking about the htaccess not redirecting properly. I’ve decided to go the route with sessions and now I just need my 404 page on my admin to redirect to my index.php which I’m working on right now. Only by logging in via my form will the session be activated (based off your diea)

Put the .htaccess in to make the 404 page go to error.php. However the URL is still displaying the originally requested URL?! I like my setup right now HOWEVER I do not want the URL to stay where it is. The page CONTENT loads error.php however the address bar is not being updated.
This is my htaccess

# Use PHP5.4 as defaultAddHandler application/x-httpd-php54 .php
RewriteCond %{HTTP_HOST} ^codefundamentals.com [NC]
RewriteRule ^(.*)$ http://www.codefundamentals.com/$1 [L,R=301]


ErrorDocument 404 /error.php

After some research, it appears it wouldn’t be a good idea to have the redirect to a single page for all my errors for the search engines. I guess I’ll leave it be for now. Thanks everyone for the help.

I stress once again that now would be an excellent time to adopt a PHP Framework. Frameworks alleviate the teething problems that you are experiencing and make the site extremely flexible and OH so much easier to expand.

Not only but also, switching to another version of your site requires only a single line changed in the root index.php and all the SEO links remain valid. Below is a link to the previous version of my site. The links on the old site all still work but because every link is processed through the new root index.php file (which has the path set to the latest version) the old links will be redirected to the new version. No loss in SEO Brownie Points!

New site versions can be developed locally, uploaded, tried, tested and switched instantly by changing a single line in the root index.php file. There is never a need for an “Under Construction” banner. Wouldn’t it have been nice to be showing your old site while developing your latest version?

Installing a PHP Framework is very easy, takes less than five minutes and your existing pages can easily be included.