Welcome Guest, Not a member yet? Register   Sign In
403 in Controller Subdirectory access
#1

(This post was last modified: 06-23-2020, 01:21 PM by inumaru.)

This is some of my syntax in Routes file:

PHP Code:
$routes->add("admin""Admin/Login::index");
$routes->add("login""Admin/Login::index");
$routes->add("admin/home""Admin/Admin::index"); 


And my folder structure for Controller is like :
[Image: yjj7M.png]

Both this link is fine when I access it:

Code:
http://mydevsite.vhost/admin/home
http://mydevsite.vhost/login


But this link:
Code:
http://mydevsite.vhost/admin

Got a 403 forbidden, is this normal? how can I redirect user who access that link to somewhere else?
Reply
#2

You also need to pass the sub-folder name.

Code:
http://mydevsite.vhost/admin/admin/home

You need also to pass along the sub-folder name in the uri.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

In this type of structure I usually use something like this:

Taking advantage of the default controller As Home and the default method index.

Code:
- Controllers
    - Admin
        - Admin (Change to Dashboard)
        - AdminControler
        - Login (Change to Home)
        - SecureThis

In routes:

PHP Code:
$routes->group('admin', ['namespace' => 'App\Controllers\Admin'], function($routes)
{
    $routes->add('/''Home::index');
    $routes->add('/dashboard''Dashboard::index');
   { ... }
}); 

So your routes would look

Code:
http://mydevsite.vhost/admin/ - To login
http://mydevsite.vhost/admin/dashboard - To after login
http://mydevsite.vhost/admin/* - All after login

Or

Code:
-   Controllers
    -   Admin
        -   Home (Dashboard)
        -   AdminControler
        -   SecureThis
    -   Auth
        -   Home (Login)
            { All login features, forgot, reset password }

PHP Code:
$routes->group('auth', ['namespace' => 'App\Controllers\Auth'], function($routes)
{
    $routes->add('/''Home::index');
// { ... }
});

$routes->group('admin', ['namespace' => 'App\Controllers\Admin'], function($routes)
{
    $routes->add('/''Home::index');
// { ... }
}); 

Routes: 

Code:
http://mydevsite.vhost/auth/ - To login
http://mydevsite.vhost/auth/forgot_password - To forgot

http://mydevsite.vhost/admin- To after login, dashboard
http://mydevsite.vhost/admin/user - After login, assuming a controller called User

I think it becomes more readable. But, just a personal opinion.
Reply
#4

(06-24-2020, 04:27 AM)InsiteFX Wrote: You also need to pass the sub-folder name.

Code:
http://mydevsite.vhost/admin/admin/home

You need also to pass along the sub-folder name in the uri.

Well, I have no problem accessing the "admin/home", you see my question was how to redirect people accessing "/admin" to somewhere else as I got 403 when accessing it,
btw I also set this in my route:
PHP Code:
$routes->setAutoRoute(false); 
Reply
#5

(06-24-2020, 05:53 AM)MatheusCastro Wrote: In this type of structure I usually use something like this:

Taking advantage of the default controller As Home and the default method index.

Code:
- Controllers
    - Admin
        - Admin (Change to Dashboard)
        - AdminControler
        - Login (Change to Home)
        - SecureThis

In routes:

PHP Code:
$routes->group('admin', ['namespace' => 'App\Controllers\Admin'], function($routes)
{
    $routes->add('/''Home::index');
    $routes->add('/dashboard''Dashboard::index');
   { ... }
}); 

So your routes would look

Code:
http://mydevsite.vhost/admin/ - To login
http://mydevsite.vhost/admin/dashboard - To after login
http://mydevsite.vhost/admin/* - All after login

I think it becomes more readable. But, just a personal opinion.

Thank you for the opinion, but my problem persist, lets say I change the file name and class (already try many things tough):

Code:
- Controllers
    - Admin
        - AdminControler
        - Home
        - Login
 
the route as your suggestion is set to:
PHP Code:
$routes->group('admin', ['namespace' => 'App\Controllers\Admin'], function($routes) {
    $routes->add('/''Admin/Login::index');
    $routes->add("home""Admin/Home::index");
}); 

the result is:
Code:
http://mydevsite.vhost/admin/ - 403 forbiden
http://mydevsite.vhost/admin/dashboard - working fine To after login

This 403 forbidden is my main problem, I have tried to set:
Redirecting Routes, change file and class name
both not working but if I change the folder name to something else it works, is the name "Admin" cannot be used?
Reply
#6

I'm not sure if this actually matters (CI might fix it internally), but according to docs CI routes use backslashes (\) to delineate namespaces and forward slashes (/) to delineate the trailing function/argument list. So
Code:
$routes->add("admin", "Admin/Login::index");

should be 


Code:
$routes->add("admin", "Admin\Login::index");

Again I don't know if this would actually cause you a problem. I think the real issue is this specific line creates a route for a file called admin, not the directory itself. I think to work properly that route would need an additional forward slash:


Code:
$routes->add("admin/", "Admin\Login::index");

This is one level shallower than yours but these routes from an example machine do basically the same thing:


Code:
$routes->get('/', 'Home::index');
$routes->get('login', 'Login::index');
$routes->get('eula', 'Login::eula');
$routes->get('overview', 'Overview::index');

Any requests to <baseURL> or <baseURL>/ are served by CI's 'default' Controller, <baseURL>/login by the Login controller, etc. 

The other thing I'll say is that with 403 errors, you should pretty much go straight to whatever is hosting the application itself, e.g. Apache, Nginx, etc. Look through those access/error logs for a relevant message, it could be something not directly related to CI, like bad file ownership or permissions.
Reply
#7

(06-24-2020, 10:15 AM)schertt Wrote: ...

The other thing I'll say is that with 403 errors, you should pretty much go straight to whatever is hosting the application itself, e.g. Apache, Nginx, etc. Look through those access/error logs for a relevant message, it could be something not directly related to CI, like bad file ownership or permissions.

This really help. Thank you for pointing this, as I finally found out why I got 403 when I access "/admin".
The problem occurred because I have a folder in my public directory which name is "admin".
I put this folder as a place for my css, js, etc for the admin and that is why it got 403 response.
So when I access
Code:
http://mydevsite.vhost/admin
it search index.php inside my public/admin folder (or at least that what the apache error was reporting) but instead of 404 I got 403.
Thank you everyone for the response.
Reply
#8

Ah that makes sense. Glad we could help.
Reply
#9

(06-24-2020, 11:09 AM)inumaru Wrote:
(06-24-2020, 10:15 AM)schertt Wrote: ...

The other thing I'll say is that with 403 errors, you should pretty much go straight to whatever is hosting the application itself, e.g. Apache, Nginx, etc. Look through those access/error logs for a relevant message, it could be something not directly related to CI, like bad file ownership or permissions.

This really help. Thank you for pointing this, as I finally found out why I got 403 when I access "/admin".
The problem occurred because I have a folder in my public directory which name is "admin".
I put this folder as a place for my css, js, etc for the admin and that is why it got 403 response.
So when I access
Code:
http://mydevsite.vhost/admin
it search index.php inside my public/admin folder (or at least that what the apache error was reporting) but instead of 404 I got 403.
Thank you everyone for the response.



I made the same mistake of naming a folder admin under the public folder for css and js. I have been searching a solution for past two days. This worked now. 
Reply
#10

(06-25-2020, 09:42 AM)schertt Wrote: Ah that makes sense. Glad we could help.
Thanks cause I eneded up with exact same problem and didn't realize I had the folder in public with same name.   Now its working
Reply




Theme © iAndrew 2016 - Forum software by © MyBB