Wolen Wolen - 1 year ago 234
PHP Question

Laravel 5 bad behaviour while removing trailing slash

I've created a Laravel project under mywebsite.com/laravel/. When I go to mywebsite.com/laravel/test, everything is ok, but when I go to mywebsite.com/laravel/test/, I'm redirected to mywebsite.com/test.

I have the files index.php and .htaccess in my /laravel directory. This is my .htaccess file:

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews

RewriteEngine On

# Redirect Trailing Slashes If Not A Folder...
RewriteBase /laravel
RewriteRule ^(.*)/$ /$1 [L,R=301]

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

I have no idea why
doesn't work. I've tried /laravel, /laravel/ and laravel; nothing worked.

My routes.php file

Route::get('/test/{name?}', 'MainController@index');

Route::group(['middleware' => ['web']], function () {

Answer Source

The solution is change

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


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

And in my case clear cache in my browser :).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download