Is there any way to remove this public from the URL? - laravel

I am using laragon server for php 7.2 laravel. I have tried a lot to remove public keyword from my url http://localhost/myLaravel/public/ by its normal method of copying .htaccess folder in my laravel project directory as well as by copying and renaming server file to index.php. But it didn't work.
whenever i write this url: http://localhost/myLaravel/
it give me following error:
Forbidden
You don't have permission to access /myLaravel/ on this server.
Any solution?

Laravel app is not meant to be accessible in a subdirectory by default. You can do one of the following:
point your domain to the myLaravel/public directory, if you want a dedicated domain or subdomain
move the public directory to the root of your Laravel app, as described here: How to make public folder as root in Laravel?

You can use this htaccess code in laravel root directory.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

Related

Deploy Laravel app on shared hosting properly?

What am I doing
Open FTP -> public_html -> laravel folder.
Copy all files from local directory to laravel folder
Open URL of the site and get this on the screen
https://i.stack.imgur.com/IbsvQ.png
Problem is simple, it opens directory, and not lead to index.php in public/ directory.
I tried everything I found in Google, like:
Move .htaccess file from /public to laravel directory and change it, like this:
<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
It works, but not completely, it load site, but it doesn't load .css and .js (static files) from public directory, which is normally, after I moved out .htaccess files, so I should change asset() everywhere in the project and add "public/", which doesn't look OK for me.
Instead of moving .htaccess I tried to edit index.php file, like most of the tutorials show:
require __DIR__.'/../vendor/autoload.php';
to
require __DIR__.'/../laravel/vendor/autoload.php';
and
$app = require_once __DIR__.'/../bootstrap/app.php';
to
$app = require_once __DIR__.'/../laravel/bootstrap/app.php';
It doesn't work.
Is there a way to handle this, without edit asset() everywhere in my project ?
Option 1
The best way to deploy, if you do not have access to the ssh but the ftp is:
Copy and paste the project on the parent folder of public_html
Wait for the upload, I recommend you to separate in an appropiate mode your dependencies in order to no install devDependencies in your prod site (this also applies for your JS assets).
once the entire laravel site is uplodaded, then you should have to delete the public_html folder and then from public make a symlink this will create a reference from your public folder and apache will try to access to the reference associated with the new public_html symlink.
Option 2
If you have access to your server through SSH, then I strongly recommend you to check if you have git available on your server, then you would be able to pull your master branch (this should be prepared for production). And also create the symlink using command line.
Also if you have SSH you will be able to prepare your project as it should with the commands you need to make it work.
I think the best way to deploy on shared hosting is to create a project folder outside the public_html folder
And add all you project files there
Then move all the files in project public folder to public_html with laravel default htacess file
Now open the index.php file you have moved to public_html
And define a proper path to project director outside the public_html directory example
require __DIR__.'/../your_project/vendor/autoload.php';
Change all path like this.... I hope its helps you

Public url issue in aws in laravel

I have hosted my site on aws through ftp,
now when i access this
url : http://X.XXX.XXX.X/projectfoldername/
i can access the page after i use
http://X.XXX.XXX.X/projectfoldername/public
but i want to remove public changed htaccess file also but when i upload it on cpanel it is working.
.htaccess file :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,QSA]
</IfModule>
Any help is highly appreciated
Nearly all Linux OS's have a .conf file that you can use to tell it which folder is the public folder. This needs to be updated for Laravel to work.
From here (https://bobcares.com/blog/cpanel-change-root-directory/), for cPanel:
4. Updating cPanel files
cPanel/WHM does not have an option to change the document root of the main domain via any interface. By default it is the public_html folder in the user home directory.
In certain rare scenarios, we have customers who want their document root to be changed to another folder than public_html.
This has to be done from the backend by updating the cPanel user account file at /var/cpanel/userdata/username/domainname .
The following parameters are updated with the desired directories.
documentroot: /home/steinknu/public_html
scriptalias:
-
path: /home/steinknu/public_html/cgi-bin/
For sites with SSL installed, the configuration file for SSL is also updated accordingly.
This file is available at the location /var/cpanel/userdata/username/domain_SSL .
After making the changes, the user data cache is updated and Apache configuration file is rebuilt to reflect the changes in the corresponding files.
The Apache and PHP-FPM services are restarted and we verify the relevant configuration files to ensure that they are all updated properly.

Cant' access laravel public folder on shared hosting

I recently installed a Laravel app on an OVH shared hosting (pro so I have ssh access). This is my installation structure :
user
app
public
www
Site access is under 'www' so I created a symbolic link there:
ln -s /home/user/app/public /home/user/www/index.html
The symlink is working and pointing to the laravel public folder when I check in Filezilla.
However when trying to access the site I am getting a 403 Forbidden You don't have permission to access this resource.
What am I doing wrong here ?
your symbolic link is problem... you are pointing to index.html instead of index.php
try this:
ln -s /home/user/app/public /home/user/www/index.php
and Create and put this .htaccess file in your laravel installation(root) folder.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
I couldn’t get this to work, it is not a laravel related problem by the way, I assume ovh doesn’t allow symlinks to a protected folder on shared servers. A shame really but I ended up installing laravel in the www folder and preventing access to root with an .htaccess
You can define a multisite with app/public as root folder. Also, I wouldn't have a link to a file like index.html, but either to a folder like ../app/public, e.g. to ensure __DIR__ works correctly in php. (By the way, Laravel does not have an index.html).
I had a similar issue on an OVH shared server: 403 forbidden, You don’t have permission to access this resource when accessing files in Laravel's public storage.
Tweaking .htaccess to allow from all or filesystem access control permissions to 777 rwxrwxrwx had no effect.
For me, the solution was to replace the absolute link /home/accountname/www/***/storage/app/public created by php artisan storage:link by the relative link ../storage/app/public

Secure laravel hosting in shared hosting with ssh access

I have access to bluehost unlimited hosting, with many domains on it, including my own.
I have SSH access.
I have deployed a laravel web app by putting the app, bootstrap and vendor folders in a folder outside the main public_html directory, and laravel's public folder contents in my domain folder, adjusted the include routes, and it's working fine.
1) Now, I would like to have all laravel related files in my domain folder but don't know how to make it secure, i.e. pointing the domain to a laravel's public folder inside my domain folder, instead of root of my domain folder, thus preventing public access to app, bootstrap and vendor folders.
I'm not sure if I explained it clearly, this is what I'm trying to achieve:
public_html/
some_domain_folder/
my_domain_folder/
app/
bootstrap/
public/
vendor/
and for my domain to point to my_domain_folder/public so the rest of laravel's files are inaccessible to public.
How can I do that?
2) If I succeed in doing 1) would I be able to execute php artisan commands via SSH?
Thanks,
Petar
Point your domain to the domain folder as normal. Then you can use a .htaccess file inside of my_domain_folder to re-write requests to /public
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
You should be able to run php artisan commands via SSH that bluehost gives you just fine, .htaccess only affects people accessing your site via their browser, not via the command line.

laravel 4 using a subdomain

I am running laravel on a subdomain i.e. subdomain.mysite.com
I have setup my structure like this:
username
-public_html
--subdomain
---public
Basically, when I created my subdomain the path I set was subname/public so the subdomain points to the public folder. I have uploaded laravel to subname so when someone goes to the subdomain url it looks at the public folder and it shows the laravel You have arrived page.
Now when I browse to mysite.com/subname it gives me a forbidden page and when I browse to mysite.com/subname/public it shows the laravel you have arrive logo.
Is it safe to set-up laravel on a subdomain this way or am I exposing the app / model / controller folders etc?
Is this just very bad and should i move the laravel core files above public_html?
I hope this makes sense
Thanks
With proper Apache configuration (or any other server you use) and .htaccess ( in case of Apache) you can restrict the access to your core directories irrespective of where your files are located.
I do not know about your server configuration and which folders you have access to, but in case you have access to other directories, you can set up laravel in somewhere else other than under public_html and set up public directory to be visible, just like you did when setting up it as a sub-domain.
If you want to setup as a sub-folder under mysite.com you can keep the Laravel installation somewhere else, and provide symbolic link under public_html like this
ln -s /path/to/laravel/public /username/public_html/subdomain
In such case you need to add following line to .htaccess file.
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /subdomain # Add this line
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Hope this helps.

Resources