Laravel slash after url redirects to root folder

情到浓时终转凉″ 提交于 2019-11-29 11:16:42
alexrussell

If you specify those routes in the opposite order it should work. Do your most specific routes first, and the more generic ones later. Think about it as a "catch this, this, and this, and then fall back to this generic one when none of the above match".


Your new problem lies in the (bad, IMO) redirect rule that Laravel 4.1 ships with. The rules very much assume that your Laravel public directory sits on the document root of the server. Here's the rule:

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

As you can see, that's saying "if the current URL ends in a slash, just redirect it to /{url}", thus getting rid of the slash. This is all well and good, just as long as your installation sits on your server's document root.

You can fix this by specifying a RewriteBase (in your case RewriteBase /laravel/public) above that rule in your public/.htaccess file, but of course, this will now change from environment to environment. This is why i consider the changing of Laravel 4.0's RedirectIftrailingSlash functionality to be a .htaccess rule to be a bad idea.

Alternatively, you can remove that RewriteRule entirely and try not to worry about the fact that your app will now respond on both URLs /laravel/public/admin and /laravel/public/admin/.

Check if you have folder "admin" in your public folder. Laravel automaticaly redirects if there is one.

Since the order matters, try this

Route::group(array('prefix' => 'admin', 'namespace' => 'Admin', 'before' => 'auth'), function()
{
    Route::get('/', 'DashboardController@main');
});

Route::get('/{slug?}', 'PageController@view');

Redirection problem Laravel redirects from URL with trailing slash to URL without trailing slash - default behaviour. (If you enter URL without trailing slash -> no redirect needed) Actual redirection is not the problem. The problem is, Laravel cannot guess right "base" URL and generates wrong redirect URL for you.

Open up app/config/app.php and set url entry to match root of your project. I guess it is http://www.mysite.com/laravel/public in your case.

The following RewriteRule works for me. I had to remove the / in /$1 from alexrussel's answer above. Including the / redirects me to root folder.

#For http://localhost/work/proj1/public/a/ RewriteBase is /work/proj1/public
RewriteBase /repo/qs/public
RewriteRule ^(.*)/$ $1 [L,R=301]

I am using laravel v4.2, apache v2.4.7, php v5.5.9 on Linux Mint 17 64-bit.

If you know the directory name, make a rule for it. This works fine for me:

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

   RewriteEngine On

   RewriteRule ^(forum)($|/) - [L]

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

   # Handle Front Controller...
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteRule ^ index.php [L]
</IfModule>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!