2

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.

tetsujinsan
  • 93
  • 2
  • 8

3 Answers3

3

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.

Community
  • 1
  • 1
tetsujinsan
  • 93
  • 2
  • 8
0

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.

Hendra Nucleo
  • 591
  • 4
  • 18
  • I have no problem to configure my local machine with a vhost. The problem is when I deploy my project to shared hosting the public\ previx will needed for all URL's and I do not want that – tetsujinsan Nov 10 '14 at 23:26
  • Try this for your ht access Options +FollowSymLinks RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L] AddType application/x-httpd-php53 .php – Hendra Nucleo Nov 10 '14 at 23:29
  • Unfortunately that did not work. I assume Htaccess is working correctly because application is launched but the routing is not recognised so there is somethig incorrect in this condition: Route::get('/', 'HomeController@showWelcome'); – tetsujinsan Nov 10 '14 at 23:46
  • How do you install laravel on your host? You move out the system file from public_html folder? and keep what inside public folder only inside your web folder? if yes, did you set the path for your index.php on line require __DIR__.'/../bootstrap/autoload.php'; & $app = require_once __DIR__.'/../bootstrap/start.php'; to correct one? – Hendra Nucleo Nov 10 '14 at 23:57
  • Something like this. Without correct paths I would get Fatal Error but instaed the application runs and exception is thrown. All file from Laravel has been copied into public_html folder. The structure goes like this: public_html/app, public_html/bootstrap (...) public_html/public. The last one contains index.php. So I have htaccess files in both public_html and \public folders. Please refer to main post to see the configuration of this files. – tetsujinsan Nov 11 '14 at 00:48
  • @tetsujinsan its litle bit blur if im not to see real structure. If you dont mind add my skype hendra_nucleo and state you are from SO so i will confirm your add. Hope i can help your quite interesting problem. – Hendra Nucleo Nov 11 '14 at 00:50
0

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.

kingprawn
  • 326
  • 2
  • 5