php - Laravel 和 htaccess 的重定向 URL 问题

标签 php laravel .htaccess redirect url-rewriting

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

我在公共(public)文件夹 (Laravel) 中有文件夹名称 admin(来自下图)。

enter image description here

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

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

enter image description here

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

enter image description here

我正在寻找,当我点击 url 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 的建议更改目录结构将是一个更好的解决方案。

关于php - Laravel 和 htaccess 的重定向 URL 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63394435/

相关文章:

javascript - 如何实现滚动时自动加载更多数据的功能?

php - 使用 PHP/MYSQL 进行基于对话的消息传递?

javascript - 如何更改Laravel + Vue.js的动态baseUrl?

javascript - AJAX Post 请求不适用于 Laravel

php - 拉维尔 Jquery : URLs are being read as &quot;

php - .htaccess 子域重定向 + wordpress

php - 如何在 CakePHP 3 中使用 postGIS

php - 关联数组(2 伙伴)

.htaccess - 将 MyBB 页面重定向到 vBulletin

php - 如何使用 .htaccess 删除 url 之间的扩展名