5

I create virtual host for my project in local xampp. Url: http://kooltech-us.com/

I have folder's name admin (From picture below ) in public folder (Laravel) .

enter image description here

in my project my admin login url http://localhost/admin/login .

I hit url http://localhost/admin/login to go to admin login page .. it's okay.. but when i hit this http://localhost/admin . it takes me to the public/admin folder . That means show me public Directory listing.

enter image description here

For prevent access Directory listing or public/admin I write in Options - Indexes code in .htaccess file .it's give me 403 Error ( Access forbidden! ) in general. it's work.

enter image description here

I looking for that when I hit the url http://localhost/admin it will take me to http://localhost/admin/login .

Note:

  1. I did not change folder structure. (Laravel Default Folder Structure Exist).
  2. Only one line code I write in .htacees file : Options - Indexes . that give me 403 Error ( Access forbidden! ) in general.
  3. Version "laravel/framework": "5.8.*"

I write in web.php but it's not working ..

Route::get('/admin', function () {
  return redirect('/admin/login');
});

if need more explanation please comment..

20
  • 4
    Default routing setup is to ignore any request that matches an existing file or folder below the public resources folder, so this request does not make it into Laravel here in the first place. You will need to modify the URL rewriting in the main .htaccess accordingly, so that this request gets handed over to Laravel despite matching a physically existing folder. Commented Aug 13, 2020 at 12:41
  • 3
    remove admin folder from public Commented Aug 15, 2020 at 13:17
  • 1
    What is in the admin folder? what are you trying to get to? It would be better if you pointed to an AdminController@index method which would deal with the request. As CBroe said above the routing is ignoring the request because of your file structure which is why @flakerimi suggested moving your admin directory. Commented Aug 17, 2020 at 8:05
  • 1
    What is in your admin directory? Do you still require normal authentication or is it just for admins? We need to see more of your code and have a better understanding of what you are trying to achieve. Yes you can achieve this by editing your .htaccess but depending on your requirements there is other ways of doing this. Commented Aug 17, 2020 at 8:59
  • 1
    @AbdullahAlNoor you must have changed the folder structure as laravel does not ship with an admin directory within it's public directory. For us to be able to help you must listen and answer the questions that are being asked. Commented Aug 17, 2020 at 12:08

1 Answer 1

2
+50

To modify the .htaccess you would need to add something like this to your .htaccess This will redirect all request from /admin to /public but still show as /admin which will allow your redirect in web.php to work:-

RewriteRule admin /public [L] 

Which would give you:-

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On
    
    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule admin /public [L] 
    RewriteRule ^ index.php [L]

    
</IfModule>

However I still feel that this is bad practice, and changing your directory strcture as @flakerimi sugested would be a much better solution.

Sign up to request clarification or add additional context in comments.

14 Comments

Please add all explanation to your answer instead of linking to external ressources
@Nico, there is a lot to explain here.. why duplicate what is already written on the posted website? Am I not allowed to refer to external sites?
No, linking to external pages is not allowed. They might change or be removed completely, which would make your answer worthless
@Chris please review my question .. if something is not clear .. please comment...
@Chris did you try it ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.