Laravel slash after url redirects to root folder

11,996

Solution 1

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/.

Solution 2

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

Solution 3

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.

Solution 4

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.

Share:
11,996
user1507868
Author by

user1507868

Updated on June 24, 2022

Comments

  • user1507868
    user1507868 almost 2 years

    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.

  • user1507868
    user1507868 about 10 years
    I tried that and it works. Thanks! But now i have a second problem. When i go to /admin it works. But when i go to /admin/ it redirects me to localhost/admin instead of the public folder of Laravel.
  • Andreyco
    Andreyco about 10 years
    Edit you question please and post FULL url you are trying to open and FULL url you are redirected to.
  • alexrussell
    alexrussell about 10 years
    @user1507868 updated my answer with an answer for the second part of your question.
  • user1507868
    user1507868 about 10 years
    I changed the url to in app/config/app.php but Laravel still redirects me to the root folder.
  • Deepak Goyal
    Deepak Goyal almost 10 years
    i have same problem if find any solution than let me know
  • abarisone
    abarisone almost 9 years
    Could you please elaborate more your answer adding a little more description about the solution you provide?
  • Niladri Banerjee - Uttarpara
    Niladri Banerjee - Uttarpara over 8 years
    Yes, it works. Also, clear cache before visiting the pages.
  • craig.tadlock
    craig.tadlock almost 8 years
    Responding to both urls is very bad practice for SEO as its see as duplicate content.
  • alexrussell
    alexrussell over 7 years
    This is quite probably true, though I'd just like to say that if I was designing a search engine ranking algorithm right now, one thing I'd definitely do is ensure that /some/url and /some/url/ would be classed as the same thing (especially if the content is the same) and just form a convention (no slash IMO) for the canonical version. As such, despite being far from a search engine ranking algorithm expert, I'd be surprised if search engines don't already do this. That said, this 'fact' is touted a lot and I don't think I've ever heard anything to the contrary other than my logic so YMMV.