【问题标题】:Laravel 7 - How to remove trailing slash URLs from my application?Laravel 7 - 如何从我的应用程序中删除斜杠 URL?
【发布时间】:2023-04-09 20:02:01
【问题描述】:

我已经尝试更改 .htaccess 文件,尝试了几种解决方案,但没有任何效果。每当我向 URL 添加尾部斜杠时,它会打开另一个页面,这不应该发生,向 URL 添加尾部斜杠应该将其重定向到没有尾部斜杠的 URL。我尝试了以下解决方案:

Solution 1

Solution 2

我想要实现的是 http://127.0.0.1:8080/login/ 应该为我的应用程序中的每个 URL 重定向到 http://127.0.0.1:8080/login

public/.htaccess

<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
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [R=301,L]

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

routes/web.php

<?php

use Illuminate\Support\Facades\Route;

// Navigation Menu
Route::get('advisor', 'AdvisorController@index');
Route::get('investment', 'InvestmentController@index');
Route::get('investor', 'InvestorController@index');
Route::get('product', 'ProductController@index');
Route::get('rate', 'RateController@index');
//  Stop registration other functions
 Auth::routes([
    'register' => false, // Registration Routes...
    'reset' => false, // Password Reset Routes...
    'verify' => false, // Email Verification Routes...
  ]);
// Login
Route::get('/', function () { 
  return redirect('login');
});
// Dashboard
Route::get('/home', 'HomeController@index')->name('home');
//admin users
Route::resource('/admin/users', 'Admin\UsersController', ['except' => ['show', 'create', 'store']]);

错误截图:

没有斜杠,我的应用程序 URL:

现在,如果我在 URL 中添加一个斜杠,我会得到:

【问题讨论】:

    标签: php laravel .htaccess redirect


    【解决方案1】:

    最后,我能够使用中间件来做到这一点,编辑 .htaccess 以某种方式无法在本地工作,或者它在我的应用程序中不起作用,无论如何,请检查以下解决方案:

    if (preg_match('/.+\/$/', $request->getRequestUri()))
     {
        return Redirect::to(rtrim($request->getRequestUri(), '/'), 301);
     }
    

    参考 here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-09
      • 1970-01-01
      • 1970-01-01
      • 2019-03-01
      相关资源
      最近更新 更多