Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9031287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:41:39+00:00 2026-06-16T07:41:39+00:00

I’d like to restrict access to a folder of controllers that are used for

  • 0

I’d like to restrict access to a folder of controllers that are used for admin purposes only. I’ve tried a number of ways and not coming up with a solution. These controllers are behind password protection. But, I’d like to just remove it from view if someone happens to stumble upon the right directory. Can this be done? I’d rather not do it from htaccess. I have access to the apache config files, so I’d like to handle it there.

Does it have anything to do with the way Codeigniter routes? Or, am I just way off?

This what I’m using that doesn’t work

<Directory /var/www/application/controllers/folder/>
  Order deny,allow
  Deny from all
  Allow from xxx.xxx.xxx.xxx
</Directory> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T07:41:40+00:00Added an answer on June 16, 2026 at 7:41 am

    Due to the way we re-write urls to work with CI, you’d never match your Apache config because you’re actually requesting index.php?{args}. If you want to filter, you have to do it in CI instead. Your options are a core controller or hooks.

    A simple way to do it is to create a core controller that your admin/ area scripts extend, and check the IP there.

    Something like this:

    application/core/MY_Controller.php:

    class MY_Controller extends CI_Controller
    {
        public function __construct()
        {
            parent::__construct();
            $this->load->config('permitted_ips');
            // check visitor IP against $config['ips'] array, redirect as needed
        }
    }
    

    Then, in your ‘sensitive’ controllers, extend MY_Controller:

    application/controllers/admin/seekrit.php

    class Seekrit extends MY_Controller
    {
        public function __construct() {
            parent::__construct();
            /* at this point any invalid IP has been redirected */
        }
    }
    

    Now, if you’re already using a core controller for something else, just check $this->uri->segment() to see if they’re in a restricted area before loading the allowed IP configuration file and checking / redirecting / dying or whatever else you need to do.

    Also, there’s no need to use a constructor in your admin controllers if you don’t need one, as the parent will be constructed if one is not defined. Just be sure to call the parent if you define one.

    You could also put the white list in a database, Redis, whatever.

    Another way to do this would be by using hooks, specifically the pre_controller hook. By the time that hook is entered, all of the security and base classes have run. This would be appropriate if you wanted to protect some or all of your routes in a more granular fashion. There, you could define an array containing routes, such as:

    $protected_routes = array(
        'foo' => array(
             'allow_ip' => '1.2.3.4',
             'redirect_if_not' => site_url('goaway')
         )
    )
    

    Then, in your hook class (or function) match the first segment (my example is just a function):

    $CI = get_instance();
    $CI->load-config('my_hook');
    $protected_routes = $CI->config->item('protected_routes');
    $segment = $CI->uri->segment(1); // foo
    if (in_array($segment, $protected_routes)) {
    
       // grab $protected_routes[$segment] and work with it
    }
    

    This has the advantage of not cluttering up your core controller as many people use that as a means of sharing code between methods. However, the hook will run on every request which means adding another two file loads to bootstrap.

    I used the hook method on a large RESTful service to protect certain endpoints by requiring additional headers, and enforcing different kinds of rate limiting on others. Note, the code above is just an example of what could go in the hook, not how to set up the hook itself. Read the hooks section of the CI manual, it’s extremely easy and straight forward.

    Finally, if you really want to do it via .htaccess, you’ll have to go by the request itself. The directory application/controllers/foo is never entered, the actual request is /foo/controller/method{args}, which causes CI to instantiate the foo/controller.php class. Remember, once re-written, the server sees index.php?....

    To accomplish this, you can re-write based on the request URI pattern, something like this (have not tested, YMMV):

    RewriteRule (^|/)foo(/|$) - [F,L]
    

    Which can be used to redirect anyone accessing the virtual path to your protected controllers. This could be preferable as it prevents PHP from needing to handle it, but you lose the granularity of control over what happens when there is a match. Still, you could use something like the above re-write combined with a hook or core implementation if you have more than one sensitive area to protect.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web folder that I would like to restrict access to via
I have a folder with multiple .aspx pages that I want to restrict access
Is it possible to restrict access to a server so that only iPhone devices
I'd like to restrict write access for the master branch to only several developers,
I'm trying to restrict access to Projects that a user did not create. This
I would like to restrict access to my /admin URL to internal IP addresses
restrict access to folder for particular user . using vb.net.In an variable am passing
I'm using Spring Security in Grails to restrict access to my controllers. I have
Is there some way to restrict access to one our pages to only allow
I'd like to restrict access to a PHP file on my server. This PHP

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.