I'm new to Laravel and it seems a great framework but i have a question when it comes to routing. I want to route all get requests to one controller action named PageController@view but when the prefix is admin i want it to route to Admin\AdminController@view. I have the following code:
Route::get('/{slug?}', 'PageController@view');
Route::group(array('prefix' => 'admin', 'namespace' => 'Admin', 'before' => 'auth'), function()
{
Route::get('/', 'DashboardController@main');
});
But when i go to: /admin, Laravel treats it like a slug instead of a prefix. Is there somekind of except function for the first line or do i simply have to switch the above lines to get the right result?
New problem:
But now i have a new problem when i go to http://www.mysite.com/laravel/public/admin/, Laravel redirects me to http://www.mysite.com/admin/ instead of calling AdminController@main. When i remove the slash at the end like this: http://www.mysite.com/laravel/public/admin it works fine.
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>
来源:https://stackoverflow.com/questions/22063520/laravel-slash-after-url-redirects-to-root-folder