Notes about Laravel Route redirection: laravelroute

Source: Internet
Author: User

Notes about Laravel Route redirection: laravelroute

Problems Found

Recently, I found a strange phenomenon in my project. The configuration in my path includes the following:

Route: group (array ('prefix' => 'admin', 'before' => 'adminauth '), function () {// The Administrator logs on to Route :: get ('/', function () {return Redirect: to ('admin/login') ;}); Route: get ('login ', 'usercontroller @ adminlogin ');}

The purpose is to jump to the logon page whether the user inputs "admin" or "admin/login. However, the actual situation is that after "admin" is entered, the browser will redirect infinitely. The firebug record is as follows:

GET /public/admin/ 301 Moved Permanently GET admin  301 Moved PermanentlyGET admin  301 Moved PermanentlyGET /public/admin/ 301 Moved PermanentlyGET admin  301 Moved PermanentlyGET /public/admin/ 301 Moved PermanentlyGET admin  301 Moved Permanently....

After some searching, I finally come to the conclusion that there are two problems:

1. There is an admin directory under the public directory, which stores js and css related to the background.

2. The. htaccess rule provided by laravel is as follows:

RewriteEngine On # Redirect Trailing Slashes... rewriteRule ^ (. *)/$/public/$1 [L, R = 301] # Note this article # Handle Front Controller... rewriteCond % {REQUEST_FILENAME }! -DRewriteCond % {REQUEST_FILENAME }! -FRewriteRule ^ index. php [L]

Explain the principle:

When you access the public/admin directory, apache automatically converts it to the "public/admin/" format, which exactly meets the rewrite rules, it is rewritten back to the "public/admin" format. Because the public/admin directory does exist, the request will not reach index. php at all.

So the story is like this, because of the existence of the 301 rule, infinite redirection is generated.

Solution:

1. Do not use the existing directory name in the public directory as the route address.

2.301 Add a "RewriteCond % {REQUEST_FILENAME} before the rule }! -D ", but this will cause direct access to this directory. We recommend that you turn off the autoindex module of apache in the production environment (ER, I found that apache cannot be started when I turn off the module of wamp --!).

Supplement:

After some experiments, we recommend that you change the. htaccess file to the following:

RewriteEngine On # Redirect Trailing Slashes... RewriteCond % {REQUEST_FILENAME }! -D # prevent the real directory from causing loop redirection RewriteRule ^ (. *)/$/public/$1 [L, R = 301] # Handle Front Controller... # RewriteCond % {REQUEST_FILENAME }! -D # Prevent Users From directly opening the real directory RewriteCond % {REQUEST_FILENAME }! -FRewriteRule ^ index. php [L]

Summary

The above is all about Laravel Route redirection. I hope this article will help you in your study or work. If you have any questions, please leave a message.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.