Laravel和htaccess的重定向网址问题

问题描述

我在本地xampp中为我的项目创建虚拟主机。网址:http://kooltech-us.com/

我在公用文件夹(Laravel)中拥有文件夹的名称admin(下图)。

enter image description here

在我的项目中,我的管理员登录网址为http://localhost/admin/login

我点击了网址http://localhost/admin/login进入了管理员登录页面..没关系。 但是当我点击这个http://localhost/admin时。它带我到public / admin文件夹。这意味着向我显示公共目录列表。

enter image description here

为了防止访问目录列表或公共/管理员,我在.htaccess文件中的Options - Indexes代码中编写了代码。通常会给我403错误禁止访问!)。是工作。

enter image description here

我要寻找的是当我到达网址http://localhost/admin时将带我到http://localhost/admin/login

注意:

  1. 我没有更改文件夹结构。 (存在Laravel文件夹结构)。
  2. 我只在.htacees文件中写一个代码Options - Indexes。通常会给我403错误禁止访问!)。
  3. 版本“ laravel / framework”:“ 5.8。*”

我用web.PHP编写,但无法正常工作..

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

如果需要更多说明,请发表评论

解决方法

要修改.htaccess,您需要在.htaccess中添加类似的内容,这会将/ admin的所有请求重定向到/ public,但仍显示为/ admin,这将使您在web.php中进行重定向:-

RewriteRule admin /public [L] 

哪个会给你:-

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

但是,我仍然认为这是一种不好的做法,而将目录结构更改为@flakerimi提示将是一个更好的解决方案。