.htaccess with subfolder other than root

Asked

Viewed 2,962 times

6

I was working with this structure and with this htaccess:

structure

config/
logs/
www/
    app/
    bootstrap/
    public/
        packages/
        .htaccess
        index.php
        ...
    vendor/
    .htaccess
    ...

htaccess

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_URI} !^public
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

But I took a shared hosting where I can’t change the root, and I ended up leaving the structure this way:

config/
logs/
www/
    folder/
        app/
        bootstrap/
        public/
            packages/
            .htaccess
            index.php
            ...
        vendor/
        ...
        .htaccess

In htaccess I left the same and also made several changes, but none worked. How would I do in this case ? The page is whole white, without showing any error or anything.

UPDATE: my .htaccess inside the briefcase public, this way:

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

    RewriteEngine On

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

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

2 answers

4


Case 1: Installing Laravel in a subfolder (Symbolic Link)

Following its structure, all you need to do is create a symbolic link on www/folder/index.php pointing to www/folder/public/index.php.

To do this you can: enter the folder www/folder and rotate the commands:

ln -s public/index.php index.php

ln -s public/. htaccess . htaccess

And another symbolic link, in www/folder/.htaccess pointing to www/folder/public/.htacess.


Case 2: Installing Laravel in a subfolder (.htaccess)

Add the folder www/Folder the following file .htaccess

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /folder
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ public/index.php [QSA,L]
</IfModule>

After this change, Laravel will no longer recognize the routes in the same way, pos it breaks the url into segments and there is an additional segment called 'Folder' (name of your folder)

then the routes should follow the model:

Route::get('/folder', function()
{
    return "site.com/folder";
});

Route::get('/folder/login', function()
{
    return "site.com/folder/login";
});

Case 3: Renaming the public folder to www

You do not need and should not host your application below the public folder for security reasons. If your public folder is called www, what you must do is:

  1. Place the contents of the folder public inside www.

  2. Change the entry public in bootstrap/paths.php.

Change:

'public' => __DIR__.'/../public',

For:

'public' => __DIR__.'/../www',
  • my www is root, and the way you said it works normally, but in the case of this client I will need to insert the folder 'Folder' after root and then yes the contents of the 'Folder' inside'.

  • show @Hernandes, it worked in a way. Sorry but I forgot to specify, at http://dominio.com.br/folder. it worked perfectly exactly the way you gave it to me, but in this case I would use a domain that would be http://folder.dominio.com.br. I removed from the file . htaccess the line Rewritebase /Folder and started working, but this not recognizing my style sheets, images, etc.

  • @tiaguinhow which answer method above you used? let me know to which I can assemble the appropriate answer to your problem

  • @Ernandes used his . htaccess and left the path as 'public' => DIR.'/../public' because if I put public_html (what is actually the one of the hosting) it does not recognize. The site structure normally uploaded, the problem is in the additional files.

  • @Hernandes, Flavio gave me a hint to add the public folder on all the Assets (Asset('public/Assets...')) and it worked. Is there a way to change the path of Asset globally ? and the Basset package ?

  • @tiaguinhow you can put your Assets folder before the public. since it is not the initial folder anymore

  • sorry @Hernandes, but how do I do it ? And with Basset there is how to configure the default directory too ?

  • @tiaguinhow your hosting is cPanel / Plesk or similar?

  • In conversation with @Ernandes, we decided to change the hosting to one that has full support for Laravel.

Show 5 more comments

1

Based on the information you’ve given, it’s certainly the version of PHP:

Documentation of Laravel 4

The Laravel framework has a few system:

PHP >= 5.3.7
Mcrypt PHP Extension

Reference - Laravel 4 Doc

If not, try the solution below, related to . htaccess and hosting directories.


Laravel 4 in shared accommodation

Necessary folders

I created the following folders inside www (or htdocs / public_html - in some accommodations):

`www/NOME_DO_PROJETO`
`www/NOME_DO_PROJETO/public`

Link the project domain

When entering the client’s domain in the hosting, refer to the folder you created, but inside the sub-folder public, in this case: www/NOME_DO_PROJETO/public.

Ready!

When uploading files to the server, whether via FTP, Git or SVN, put them in the folder www/NOME_DO_PROJETO, and give appropriate permissions on public and storage.

With this, everything should work clearly.

I hope it helps.

Note: no need to change . htaccess, path.php, start.php, etc

  • Thanks @patrick-Maciel, I did so but the same problem happened, the screen is all white and does not present any message, or anything in the source code

  • @tiaguinhow Strange. Blank was not meant to be. If your files are intact (htaccess/path/start/etc), with the same paths, it was meant to work.

  • are intact yes, the only thing I noticed, is that the PHP version of the hosting is 5.3.14, I see to upgrade to at least 5.3.7 that seems to be the version compatible with L4. Let’s see if this resolves.

  • @tiaguinhow This is the problem then. I will update my answer.

  • opa, now that I updated the PHP version we had an evolution. I had to change the bootstrap/path.php file to require DIR.'/../bootstrap/autoload.php'; $app = require_once DIR.'/../bootstrap/start.php'; am now receiving the following route error: Symfony Component Httpkernel Exception Notfoundhttpexception /home/u512270631/public_html/intranetv4/vendor/Laravel/framework/src/Illuminate/Routing/Routecollection.php do I still need to change/update something else ? Thank you

  • @tiaguinhow Run a basic test on your main route, for example: https://gist.github.com/8479939. So you can already tell if it’s a problem with Laravel 4 + server, or just with its routes.

  • 1

    add "public" to the Assets path, as you had to change the path of index.php

  • Flavio, this would work but in conversation with Ernandes, it would conflict in more things. I decided to change the accommodation to one that has full support to Laravel. Thanks Patrick and Flavio, both helps were good.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.