2

How can i change http://domain.com/public/index.php to http://domain.com and can get the other routes working other than ('/') ?

Workaround 1:

vhost file:

<VirtualHost *:80>   
    DocumentRoot "/var/www/html/domain/public"
    ServerName domain.com
    <Directory "/var/www/html/domain/public">
        AllowOverride All
        Allow from All
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

htaccess file:

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

With this setup, yes i'm able to set http://domain.com but when i'm trying to invoke another route, getting a 404. The reason behind this is as you can see i've set my root folder as public. So my routes cannot reach their destinations (like the ones which are being directed to my controllers, because my controllers are not in the public folder).

Workaround 2:
If i change Document root and directory into /var/www/html/domain/ this time i'm losing my pretty url and only way i can request main page by entering http://domain.com/public/index.php.

Note that I'm using ubuntu 14.04.

What do you suggest?

---update---
Route example:

Route::get('myroute', array(
   'uses' => 'MyController@myMethod',
   'as' => 'myroute'
));

---update 2--- php artisan route:list results are

+--------+----------+---------+------+---------+------------+
| Domain | Method   | URI     | Name | Action  | Middleware |
+--------+----------+---------+------+---------+------------+
|        | GET|HEAD | /       |      | Closure | web        |
|        | GET|HEAD | myroute |      | Closure | web        |
+--------+----------+---------+------+---------+------------+
saimcan
  • 1,706
  • 7
  • 32
  • 64
  • You virtual host **should** point to the public folder, there's no need for workarounds and tricks, it should work just like it is. Can we see how you define your routes that you get 404? – thefallen Jun 09 '16 at 06:28
  • The virtual hosts slution should work fine. From your desciption it seems liek you haven;t grapsed an understanding of how the Laravel works. The entry point will always be `index.php`, even if we point virtualhost to public directory, all requests reach `index.php` file, which then bootstraps and routes the request as required. All routes should work fine. – saji89 Jun 09 '16 at 06:30
  • @TheFallen I've updated the post. – saimcan Jun 09 '16 at 06:30
  • Have you added an entry in the /etc/hosts file for domain.com? It should look like `127.0.0.1 domain.com` assuming that you haven't set any other ip for your workstation. – saji89 Jun 09 '16 at 06:33
  • @blankBird_ run **php artisan serve** from your project folder, which will create a server on http://localhost:8000 and try to access your route there. – thefallen Jun 09 '16 at 06:34
  • @TheFallen, That's just a temporary workaround, not a solution to the OP's question. – saji89 Jun 09 '16 at 06:35
  • 1
    @saji89 yep, but we'll know if the problem is the vhost or the route. – thefallen Jun 09 '16 at 06:41
  • use your Workaround 2 with below-: put a .htaccess file at root of your application and write this, it will work. RewriteEngine on RewriteCond %{REQUEST_URI} !^public RewriteRule ^(.*)$ public/$1 [L] – Laravel User Jun 09 '16 at 06:53
  • @TheFallen, That's true. :) – saji89 Jun 09 '16 at 07:20

3 Answers3

4

You need to correctly setup virtual host in you web server's config file. Set public directory as root directory for Laravel VH and restart web server.

For Apache you can use these directives:

DocumentRoot "/path_to_aravel_project/public"
<Directory "/path_to_aravel_project/public">

For nginx, you should change this line:

root /path_to_aravel_project/public;
Community
  • 1
  • 1
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • thanks, but my configuration has already been set like this. I am not able to reach my routes if i set config like this. – saimcan Jun 09 '16 at 06:47
  • Please show me URL you're trying to use output of `php artisan route:list`. Also, did you edit `.htaccess` or `index.php`? – Alexey Mezenin Jun 09 '16 at 06:59
  • I've updated the post. No, i haven't changed .htaccess or index.php by the way. – saimcan Jun 09 '16 at 07:05
  • Why your route is `myroute` but it shows `myRoute`? What exactly error do you get when you're trying to go to `http://domain.com/myroute`? Does `error.log` show anything? – Alexey Mezenin Jun 09 '16 at 07:20
  • The route is myRoute, sorry for the typo. error.log doesn't show anything. I am just getting the response from the browser: The requested URL /myroute was not found on this server. Sorry for bothering you but I'm trying to figure out what's wrong for hours. – saimcan Jun 09 '16 at 07:35
3

This is how I deal with this in my projects. There are two steps.

1.create a new .htaccess file in your /public directory with following content:

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

RewriteEngine On

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

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

2. there is a file named server.php in your project root.. (parent of app, public etc.).

rename that to index.php

And it should work.. without any hassle.

Rahul M
  • 1,498
  • 12
  • 19
  • works for me all the times.. can you please share the changes you observe after this? Or there ain't any change at all.. – Rahul M Jun 09 '16 at 08:08
0

Yes, each of those answers are nicely working, but before that I found out apache rewrite module mod_rewrite should have already been activated.

On ubuntu console: a2enmod rewrite solved my situation.

halfer
  • 19,824
  • 17
  • 99
  • 186
saimcan
  • 1,706
  • 7
  • 32
  • 64