20

I have a folder I've placed in the /public folder in my Laravel site. The path is:

/public/test
                   

the "test" folder has a file called index.html that is just a static resource I want to load from the public folder (I don't want to create a view for this within the framework--too much to explain).

I've made a route like this:

Route::get('/test', function() {
  return File::get(public_path() . '/test/index.html');
});
                   

I'm not able to get this to work. I just get an error from Chrome saying this page has a redirect loop.

I can access it this way:

http://www.example.com/test/index.html
                   

But I can't use the .html extension anywhere. The file must be visible as:

http://www.example.com/test/
                   

How can I serve a single HTML page from the Laravel public folder without having to use the .html extension?

                      
improve this question                            
asked Mar 23 '15 at 18:41                                
                                   
                               
add a comment                
       

3 Answers  正确答案                

activeoldestvotes                    
       
19

used to face this problem before, as a dirty little trick, you may rename the test folder to something else then update

return File::get(public_path() . '/to new folder name/index.html');
                       

the key is no conflict between your route url with your folder in public

improve this answer                            
answered Mar 23 '15 at 20:38                                
                                   
                               
  • This worked. As you said, the key is making sure your route path and the path (folder) in the public directory each have different names. Thanks for the help! – ChiCgi Mar 23 '15 at 20:56                                    
add a comment                    
           
       
4

For a static file, I think you're wasting resources serving it through Laravel.

There have been some bugs in the past creating infinite redirects when trying to access files/folders within public, but I think the latest .htaccess that comes with Laravel has these resolved (on Apache, at least). It looks like this

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

    RewriteEngine On

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

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>
                       

It says 'If the request is not for an existing folder, strip the trailing slash if there is one. If the request is not for an existing folder or for an existing file, load Laravel's index.php'

I then added a .htaccess file within the subdirectory to ensure that the DirectoryIndex is set so that index.html will be loaded by default on a request for a directory.

improve this answer                            
answered Jul 28 '15 at 6:43                                
                                   
                               
add a comment                    
       
2

Use:

return Redirect::to('/test/index');
                       

Either at the function at your routes file or inside a controller.

improve this answer                            
answered Mar 23 '15 at 19:37                                
                                   
                               
  • 1                                    
    That didn't work. I still get the redirect loop error. – ChiCgi Mar 23 '15 at 19:58                                    
add a comment                    
       

来自  https://stackoverflow.com/questions/29217937/how-to-route-to-a-static-folder-in-laravel