I know this is a very popular question but I haven\'t been able to find a working solution for Laravel 5. I\'ve been trying to migrate from Codeigniter for a long time, but
I have solved the issue using 2 answers:
Changing .htaccess it a bit as follows for statics:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [L,NC]
If there are any other static files needed just add the extension to the previous declared list
In Laravel 5.5 create .htacess file in your root directory and placed the following code:- Reference Link
<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>
@rimon.ekjon said:
Rename the server.php in the your Laravel root folder to index.php and copy the .htaccess file from /public directory to your Laravel root folder. -- Thats it !! :)
That's working for me. But all resource files in /public directory couldn't find and request urls didn't work, because I used asset() helper.
I changed /Illuminate/Foundation/helpers.php/asset() function as follows:
function asset($path, $secure = null)
{
return app('url')->asset("public/".$path, $secure);
}
Now everything works :)
Thank you @rimon.ekjon and all of you.
This answer is not recommended.
Instead, handling .htaccess
file is recommended.
Problem is if you type /public and it will still be available in the url, therefor i created a fix which should be placed in the public/index.php
$uri = urldecode(
parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
);
if(stristr($uri, '/public/') == TRUE) {
if(file_exists(__DIR__.'/public'.$uri)){
}else{
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
$actual_link = str_replace('public/', '',$actual_link);
header("HTTP/1.0 404 Not Found");
header("Location: ".$actual_link."");
exit();
return false;
}}
this peace of code will remove public from the url and will give a 404 and then redirects to the url without the public
Create new file called .htaccess
in root project folder and place the below code inside it :
<IfModule mod_rewrite.c>
#Session timeout
<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>
Note: If you changed server.php
to index.php
you also should rename it in above code
4 best ways to remove public from the URL.
If you used any other trick to remove the public from the URL like changes the name of server.php to index.php and changing into the core file path. Clearly, don't do that. Then why Laravel not giving the solution like this because it's not a proper way to do that.
1) Remove public from URL using htaccess in Laravel
By adding a .htaccess file into the root, You can access the website without public
<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>
2) Remove the public by creating a virtual host in your local
I am giving demo here for the Window operating system. But I will try to define a step so that anyone can easily follow the step. You can also research on google for the same for the particular operating system.
Step 1: Go to C:\Windows\system32\drivers\etc\ open the "hosts" file in Administrator mode.
Step 2: Add the following code to it. Here, I am giving you a demo of projectname.local domain name demo, you can specify any as you like. Just make it constant at every place.
127.0.0.1 projectname.local
Step 3: Now go to, C:\xampp\apache\conf\extra
for xampp users and for the wamp user "C:\wamp\bin\apache\Apache2.4.4\conf\extra"
and open "httpd-vhosts.conf"
file. Now add the following code into it.
Notes: Change the Document root as per your project also add domain name as you define into the "hosts" file.
<VirtualHost projectname.local>
ServerAdmin projectname.local
DocumentRoot "C:/xampp/htdocs/projectdir"
ServerName projectname.local
ErrorLog "logs/projectname.local.log"
CustomLog "logs/projectname.local.log" common
</VirtualHost>
Step 4: Last but the important step is to restart your Xampp or Wamp and access the url like http://projectname.local
and your Laravel will respond without public URL.
3) Remove the public by running the command in Laravel
If you are working in local then you don't need to do anything just need to run the following command from your terminal or command line tool. After that, you can access your website by provided URL by the command line.
> php artisan serve
If you are willing to run your project on particular IP then you need to run following command. If you are working on LAN then if you want to allow other people to access your website from local then you just need to check your IP address using command line by running "ipconfig" after getting your IP address run following the command.
> php artisan serve --host=192.168.0.177
If you are willing to run your project on a particular IP with particular port then you need to the following command.
> php artisan serve --host=192.168.0.177 --port=77
4) Remove the public on the hosted server or on the cpanel
After completion of the project you need to host the project on the server, then you just need to set the document root on your domain to the public folder. Check the below screenshot.
As per screenshot if you don't have any project folder into the public_html then you just need to set your document root like "public_html/public"
.
Reference taken from here