0

I have Laravel 4 application and I have some subfolder applications. Laravel 4 app is in root folder and there is a .htaccess file there with following content:

Options -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

Now, if i go to www.mysite.com/subfolder-app i get redirect loop error. I get this error for any subfolder on server

Vuk Stanković
  • 7,864
  • 10
  • 41
  • 65

2 Answers2

0

You should be able to simply change the RewriteRule to include the sub-folder:

Options -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /subfolder-app/index.php [L]

Note, however, that this won't "dyamically" change with your environment. If your localhost is running with Laravel in the webroot instead of a sub-folder, making this change to your htaccess file could make it no longer work in your localhost dev environment.

One solution to this is to not use an .htaccess file, but rather include the rewrite rule in the virtual host configuration file for your domain. The ability to do so will depend on your hosting.

That solution also has the benefit of removing what is a server-configuration (rather than an application configuration) out of our your application code repository

EDIT

Here's the final resolution the OP used, as it's not exactly what I suggested:

Options -MultiViews 

RewriteEngine On 

# Rewrite to "www" version
RewriteCond %{HTTP_HOST} !^www\. 
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] 

# Add -d check to ensure only rewrite if url 
# is not an existing directory
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^ index.php [L]
Community
  • 1
  • 1
fideloper
  • 12,213
  • 1
  • 41
  • 38
  • 1
    Fixed it by adding `RewriteCond %{REQUEST_FILENAME} !-d` after `-f` line – Vuk Stanković Aug 26 '13 at 19:58
  • Great, checks to make sure its not also a directory before running the rewrite rule. Can you add in your final solution for others? I can edit my answer with it if you'd like. – fideloper Aug 26 '13 at 20:35
  • Here it is `Options -MultiViews RewriteEngine On RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^ index.php [L]` It also redirects to www. – Vuk Stanković Aug 27 '13 at 08:09
0

So I had a similar problem where there were sub folders in my public laravel project file. When I made a request to xyz.com/sub-directory it would create a redirect loop. The way I solved that was by adding the following just before the trailing slashes:

RewriteRule ^sub-directory/([a-zA-Z0-9-_]*/)*?$ / [L,R=301]

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

One of solutions mentioned in the laravel github pages was to remove the trailing slashes but that exposes your sub-directory to the default apache directory viewer and you probably really don't want that. This method works for me as it adds the redirection rule to base path for the sub directory and any directories in that recursively (as long as they are defined by alphanumeric, - or _ symbols).

Farhan Rahman
  • 206
  • 2
  • 12