简体   繁体   中英

How to configure laravel project for production server and remove public in url

LARAVEL 5.0 | APACHE 2.4.7 | PHP 5.5.8

I know this is a pretty popular question. A lot of people saying to move public contents to root directory (which i find awkward because of security issues), rewriting the .htaccess, creating virtual host.

I tried creating virtual host because i think this is more acceptable solution.

but i'm encountering some problems with my configurations:

I'm running easyphp for the meantime, id like to test if i can get rid of the public route in url before setting this up live.

httpd.conf

NameVirtualhost *:80
<VirtualHost *:80>
    DocumentRoot "${path}/data/localweb/quantum.dev/public"
    ServerAdmin admin@localhost
    ServerName quantum.dev
    ServerAlias www.quantum.dev
    <Directory "${path}/data/localweb/quantum.dev/public">
        AllowOverride All
        Options Indexes FollowSymLinks

        Order deny,allow
        Allow from quantum.dev
        Deny from all
        Require all granted
    </Directory>
</VirtualHost>

I modified C:\\Windows\\System32\\drivers\\etc

hosts

127.0.0.1       quantum.dev

When I try to browse http://127.0.0.1/quantum.dev/ it will load the listof files and not the project inside public

http://127.0.0.1/quantum.dev/ 在此输入图像描述

Please revert all your changes to the directory. restore the default structure and configuration of the project. If i'm not mistaken and correct me if i am, you are using a shared hosting provider. If that is the case, here is a more proper way of doing so.

In your .htaccess , you need to add the following.

At the top of the file add this line:

DirectoryIndex public/index.php

and in here:

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

Change the last rule to this:

RewriteRule ^ public/index.php [L]

Now, all you need to do is to move this, and only this(the .htaccess file), to the root folder of your project. In this way, you are maintaining the laravel project structure in its original state.

put this .htaccess file near public folder

Options -MultiViews

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]

When you are selecting a directory for your domain in c-panel, simply provide path till the Laravel's public directory and not just till the Laravel's root directory. That way you don't need to change .htaccess at all.

I have just recently upload my site to the production and follow above steps. It worked very well.

This is my configuration that is working fine for me. So first of all, your Virtual host configuration should be like that :

ServerAdmin webmaster@domaine.dev
ServerName domaine.dev
DocumentRoot /var/www/mysiteweb/public/

    <Directory /var/www/mysiteweb/>
            Options -Indexes
            DirectoryIndex index.php index.html
            AllowOverride All
    </Directory>

Anf after, make this on your .htaccess laravel file :

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

Don't forget to check if the mod_rewrite is enable.

Hope that helped. Regards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM