2
votes

I've searched through Web to solve my problem but non of the solutions works for me there, so after couple of hours of struggling with Routing module I've decided to ask you for a helping hand.

Problem

I am unable to access Laravel application from outside /public directory. I need to type localhost\projects\laravel\public in my browser but what I want is to use the URL withour /public. The main reason is because I use shared hosting and have no access to apache configuration file so I'm unable to create vhost.

Background

I've installed manually Laravel framework under the: **c:\xampp\htdocs\projects\laravel** and used composer to do the rest for me.

The routing has been set to: Route::get('/', 'HomeController@showWelcome');

In my project root directory **c:\xampp\htdocs\projects\laravel** I've put a .htacces file with the following content:

<IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /projects/laravel/
        RewriteRule ^(.*)$ public/$1 [L]
    </IfModule>

    <IfModule !mod_rewrite.c>    
        ErrorDocument 404 /index.php
    </IfModule>

In my Application Root dir c:\xampp\htdocs\projects\laravel\public I have .htaccess with the following code in it:

<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>

When I type: localhost\projects\laravel\ I've got NotFoundHttpException

When I type: localhost\projects\laravel\public I've got welcome page

Any help would be appreciate, Thanks.

3
the index.php file in the public folder is what starts your applicationMauricio Trajano
Moving index.php from /public to project ROOT and changing paths in it to correct ones will couse the same problem. Application runs but NotFoundHttpException is thrown. Something is wrong with Routing configuration.tetsujinsan
@tetsujinsan What is your hosting? did u check php version?Hendra Nucleo
PHP Version 5.5.9 and I have mod_rewrite ON.tetsujinsan

3 Answers

2
votes

Finally I've got this thing working. There are two solutions that can be made.

  1. One requires to move all -public content level-up
  2. Second requires slightly changes in your shared hosting directory root structure - if allowed

Solution #1

The first that works for me that I didn't want to implement has been posted by @Wasim in this thread: Laravel 4 removing public from URL The solution is not save as the content core structure is in the same directory as application itself. This could cause some problems in future implementation.

You need to move all the content from public/ folder one level-up into project ROOT directory then replace internal paths in index.php file for correct onece. For security reasons this .htaccess file needs to be put into project ROOT directory

<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> 

Modified Laravel structure described above can be uploaded to your shared web hosting and the application should runs withour /public in yor http address.


Solution 2 (with no access to http.conf file on your shared hosting)

This solution does not requires form you to move the content of the /public folder one level-up but requires form you to have read and write access to ../ROOT directory in your shared hosting (../ROOT directory mostly contains public_html, public_ftp and other folders).

You need to move Laravel scructure into ../ROOT directory as follows:

app/
bootstrap/
vendors/
public_html/
public_ftp/
(...)

Files form /public folder goes to public_html

public_html/index.php
public_hmtl/.htaccess
public_html/packages/
(...)

Then modification for /bootstrap/paths.php is required for line with 'public' key:

'public' => DIR.'/../public' to 'public' => DIR.'/../public_html'

If someone has similar issue and this solution does not work please let me know, thanks.

0
votes

If you want without public on uri, using composer from your laravel root. Use command php artisan serve and browse localhost:8000 from your browser. It will bring you to homepage.

0
votes

Where did you get this line from?

RewriteRule ^(.*)$ public/$1 [L]

It's not part of Laravel. Try removing it or the whole .htaccess file in your Laravel root.