skip to Main Content

I am using Laravel 5.2 in my shared hosting server. I would like to remove public from url without changing server.php to index.php in root directory as doing so .env and other files are publicly visible. My .htaccess look likes below:

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

RewriteEngine On

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

# Handle Front Controller...

RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,QSA]
RewriteRule ^(css|js|images|media)/(.*)$ /public/$1/$2 [L,NC]

</IfModule>

5

Answers


  1. You need to point you domain to public folder from cpanel.

    or

    Move your .htaccess file from the /public directory to the root directory and replace it content with the following code:

    <IfModule mod_rewrite.c>
        <IfModule mod_negotiation.c>
            Options -MultiViews
        </IfModule>
    
        RewriteEngine On
    
        RewriteCond %{REQUEST_FILENAME} -d [OR]
        RewriteCond %{REQUEST_FILENAME} -f
        RewriteRule ^ ^$1 [N]
    
        RewriteCond %{REQUEST_URI} (.w+$) [NC]
        RewriteRule ^(.*)$ public/$1 
    
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^ server.php
    
    </IfModule>
    
    Login or Signup to reply.
  2. The Best Solution for this will be pointing your apache document root to laravel’s public folder.
    But as you said you are using shared hosting then that might not be possible for you.

    Now to access laravel app without public in the url follow this question
    Laravel 5 – Remove public from URL

    Also, you are concerned about other folders like app, config etc to not be accessible from the browser. Therefore to do that you can place an .htaccess
    file with contents Deny from all in every folder that you want to block access from the browser.

    Login or Signup to reply.
  3. Create new
    .htaccess file in root directory
    and paste with the following code

    <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteCond %{REQUEST_URI} !^public
        RewriteRule ^(.*)$ public/$1 [L]
    </IfModule>
    
    # php -- BEGIN cPanel-generated handler, do not edit
    # Set the “ea-php56” package as the default “PHP” programming language.
    <IfModule mime_module>
      AddType application/x-httpd-ea-php56___lsphp .php .php5 .phtml
    </IfModule>
    # php -- END cPanel-generated handler, do not edit
    
    Login or Signup to reply.
  4. In root folder make .htaccess file with:

    <IfModule mod_rewrite.c>
     RewriteEngine On
     RewriteRule ^(.*)$ public/$1 [L]
    </IfModule>
    
    Login or Signup to reply.
  5. This is the exact working solution i found. Solution is taken from here, answered by Pallav Nagar

    Create .htaccess file in root directory and place code something like below.

    <IfModule mod_rewrite.c>
        <IfModule mod_negotiation.c>
            Options -MultiViews
        </IfModule>
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} -d [OR]
        RewriteCond %{REQUEST_FILENAME} -f
        RewriteRule ^ ^$1 [N]
    
        RewriteCond %{REQUEST_URI} (.w+$) [NC]
        RewriteRule ^(.*)$ public/$1 
    
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^ server.php
    </IfModule>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search