Laravel Trailing Slashes Redirect to Localhost

laravel trailing Slashes redirect to localhost

Change your code to this:

Options -MultiViews
RewriteEngine On
RewriteBase /Testlaravel/public/

# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ $1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

URL with trailing slashes gets redirected to localhost in laravel 5

Added this and it worked!

RewriteCond %{REQUEST_URI} !^

Laravel: How do I make route helper return urls with trailing slashes?

Because Laravel will remove slash at the end of URL so you just can do it by using {{ route('home') }}/.

Reference: https://github.com/laravel/framework/blob/5.3/src/Illuminate/Routing/UrlGenerator.php#L308-L345

How to prevent multiple slashes during routing in Laravel?

To be clear, this is not a bug. A bug would imply that this is something the framework developers explicitly said shouldn't happen, but actually happens. This is mostly an undefined behaviour.

The reason this happens is because of how the request determines the current path:

In Request::path()

public function path()
{
$pattern = trim($this->getPathInfo(), '/');
return $pattern == '' ? '/' : $pattern;
}

this trims / from the current pathinfo to get the path. This is not an unreasonable thing to do as you would normally not want leading and trailing slashes, but this has the consequence that paths with many leading and trailing slashes will also work like for example https://laravel.com////docs/5.5/errors/////#http-exceptions

The reason this happens is because of the standard route validators in returned by Route::getValidators() (specifically the UriValidator which uses $request->path())

If you really must insist on "fixing" this then you can add a custom validator that checks this exact thing:

class MyUriValidator implements ValidatorInterface  {

public function matches(Route $route, Request $request)
{
$path = $request->getPathInfo();
return preg_match($route->getCompiled()->getRegex(), rawurldecode($path));
}
}

Then you can register this extra validator in your AppServiceProvider:

public function boot() {
Route::$validators = array_merge(Route::getValidators(), [ new MyUriValidator ]);
}

Odd redirect when trailing slash added to end of url

As far as I remember Laravel 4.x was the last version having a "redirectIfTrailingSlash" function. Every next version had an initial .htaccess file containing the correct set of rules to redirect urls with trailing slash (but no dirs etc).
I can see your .htaccess contains your own modifications which do this redirect. Replace your lines with the following:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301].
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]


Related Topics



Leave a reply



Submit