Laravel 5 bad behavior when removing trailing slash

I created a Laravel project under mywebsite.com/laravel/. When I go to mywebsite.com/laravel/test, everything is fine, but when I go to mywebsite.com/laravel/test/, I am redirected to mywebsite.com/test.

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

<IfModule mod_rewrite.c> <IfModule mod_negotiation.c> Options -MultiViews </IfModule> 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 RewriteBase not working. I tried / laravel, / laravel / and laravel; nothing worked.

My route.php file

 <?php Route::get('/test/{name?}', 'MainController@index'); Route::group(['middleware' => ['web']], function () { // }); 
+6
php laravel
source share
2 answers

Solution is change

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

to

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

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

+6
source share

This works for me; removing all trailing slashes from all routes, emphasizing that REQUEST_URI starts with a slash (at least in .htaccess files):

Replace:

 # Redirect Trailing Slashes If Not A Folder... RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)/$ /$1 [L,R=301] 

FROM

 # Remove all trailing slashes RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} /(.*)/$ RewriteRule ^ /%1 [R=301,L] 

This will rewrite mywebsite.com/laravel/test/ at mywebsite.com/laravel/test/ without redirecting you to mywebsite.com/test

Just don't use %{REQUEST_URI} (.*)/$ . Because the root directory of REQUEST_URI is /, the leading slash, and that would be misinterpreted as a trailing slash.

SOURCE: https://stackoverflow.com/a/312969/

+1
source share

All Articles