We're trying to remove last / from url to avoid google duplicate contents. I created a middleware for 301 redirect.
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class NonSlashURL
{
public function handle(Request $request, Closure $next)
{
if (substr($request->fullUrl(), -1) == '/') {
$trimmedURL = rtrim($request->fullUrl(), "/");
return redirect($trimmedURL, 301);
} else {
return $next($request);
}
}
}
What I want to achieve if user enter https://test.com/content/, that middleware redirect user to https://test.com/content
But if I get full url;
dd($request->fullUrl());
It gives me https://test.com/content even I enter website with https://test.com/content/.
My Answer:
be used to create web applications and websites. It provides a robust framework for building and managing web projects, with features such as routing, authentication, database management, and more. Laravel also includes a templating engine called Blade, which makes it easy to create dynamic and responsive web pages. Additionally, Laravel has a strong community of developers and extensive documentation, making it a popular choice for building web applications.
Rate this post
5 of 5 based on 5887 votesComments