Laravel root directory not public on Hostinger hosting - laravel

I currently host my Laravel website on Hostinger, but when I have a image on my page, the url doesn't go to website.com/public/img_url, but it goes to website.com/img_url, does anyone know how this works on Hostinger, and how I can fix this?
Currently my whole project is in my public_html, with no other folders besides it.

The right solution was changing my htaccess to:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

You should need to add a public inside asset function to access a public folder on the shared hosting server.
So you need to change the fetch path of files Example: asset('user/photo.jpg') to asset('public/user/photo.jpg') in each line of code where you need to fetch files from the public directory.

Related

How to hide public from url in laravel 9

I used Laravel 9 and Inertia.js + Vuejs and the project has been deployed to the cpanel. When I access to the site and it threw an error saying Not Found with code 404 so then I added a new file - .htaccess outside the public folder and the error existed. I tried to access to public folder like this https://imapp.asomaningministries.com/public and it worked but I don't want anyone to eventually access to this folder. So can I hide it?
You can try to set the document root directory as 'public' directory on the domain settings page of cPanel.
or you can use .htaccess file like that;
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain-name.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain-name.com$
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) /public/$1 [L]

How to solve 404 error after deploying laravel in server under subfolder?

I have uploaded a laravel project in server inside a subfolder under public_html directory.
For example www.xyz.com/subfolder.
Bot I am getting error 404 NOT Found.
My htaccess code
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
So, I've been fiddling a lot with VirtualHost(s), but the best solution I found does not involve them at all, and here it is.
First thing to do is to configure .htaccess files for both the app's root folder and its public folder. I found a simple working .htaccess file in this answer, which I report here:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
The default .htaccess works fine, instead, for the public folder.
The last step is to correctly configure your routes in routes.php. The workaround which works for me is quite rough, and maybe one could find some more refined solution, but still here it is.
// Get base (root) route
$base_route = basename(base_path());
Route::get($base_route, function () {
return view('welcome');
});
Route::get($base_route.'/myRoute', function() {
return view('myRoute');
});
So basically you need to prepend $base_route to every route in your app.

Routing fail on Laravel app deployed on subfolder

Ok, this is what is happening.
I've created a small laravel app (actually, still it's on the development stage).
When I upload it to the server (shared hosting managed through cPanel) I've deployed it to a subfolder inside the main website.
There is the main website (a WordPress) on the server's public_html folder, so I created a subfolder where I put my app.
Now, when you request http://oursite.com you reach the Wordpress site.
And when you request http://oursite.com/laravelapp you reach the initial page of the laravel app.
However, when I try to browse any page into the app, the routing returns me to routes inside the root web folder.
For example, if I push the login button, the petition to http://oursite.com/laravelapp/login redirects to http://oursite.com/login, and obviously, the page load fails
The funny thing is that if I reach directly public pages, it seems to work fine.
If I directly request http://oursite.com/laravelapp/public/index.php and push the same login button, a request to http://oursite.com/laravelapp/public/index.php/login is made, and it works!! Even JS and CSS links are correctly detected on that request!!
I suspect this is probably something quite stupid that I cannot see now because I'm too tired, but I will really appreciate any insight on the matter.
The index.php on my laravelapp folder is the following:
<?php
/**
* Laravel - A PHP Framework For Web Artisans
*
* #package Laravel
* #author Taylor Otwell <taylor#laravel.com>
*/
$uri = urldecode(
parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
);
// This file allows us to emulate Apache's "mod_rewrite" functionality from the
// built-in PHP web server. This provides a convenient way to test a Laravel
// application without having installed a "real" web server software here.
if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) {
return false;
}
require_once __DIR__.'/public/index.php';
And this is the code of the .htaccess file inside /laravelapp/public folder:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Maybe I would be able to fix this using a subdomain like myLaravelApp.oursite.com?
Ok.
I fixed this problem copying the .htaccess on laravelApp/public to laravelApp.
It seemed that the root folder needed some type of redirection, and this worked like a charm.

Codeigniter 404 not found error when accessing anything inside application folder

I am having a problem after hosting my codeIgniter project on live shared hosting server.
Before doing that, I changed few things on my local server.
.htaccess file-
Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
.config.php inside config folder-
$config['base_url'] = '';
$config['index_page'] = '';
to let it work properly on my local server without writing index.php in URL. But when I moved it to live server, I changed base_url as-
http://crm.sachinartani.com
as I am hosting it on my subdomain, but it always output-
404 Page Not Found
The page you requested was not found.
I watched all videos on YouTube and traversed all questions of StackOverflow but none of the solutions helped me. There is a folder aside application folder namely inc which contains CSS and JS files I am using in my project. I am able to access those files by URL as - http://crm.sachinartani.com/inc but I cannot access any controllers and views like this.

Moving codeigniter project from localhost to cpanel

I am trying to run my codeigniter website on the server.But I don't have a clue that what settings should I perform.
I find the following link to move the files and do the specified settings but still its not showing me the home page of my website.
Here's what I've done till now.
I moved the codeigniter website's application and system folder on the root directory while my .htaccess file along with my assets and index.php is inside public_html.
Here's my directory structure.
-/home/mysite
-public_html
-index.php <-- the path for system is set to '/home/mysite/system' and for application folder is '/home/mysite/application'
-.htaccess
-system
-application
I've performed the following settings as mentioned in the link.
$config['index_page'] = "";
$config['base_url'] = "http://myurl.com/";
$config['server_root'] = $_SERVER['DOCUMENT_ROOT'];
$config['uri_protocol'] = 'AUTO';
$route['default_controller'] = "home_controller";
Here's my .htaccess file.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Now I don't know why my home page is still not appearing when I enter the url to my site.
upload your application folder, system folder, assets folder, index.php, .HTACCESS inside your public_html folder
You need to do few steps:
update 'application/config/config.php' for base_url
update 'htaccess' file for RewriteBase (if needed)
update file permission (if needed)
update 'aplication/config/database.php' for database settings
if still you face issue, then you need to debug like:
print something in index controller and walk through along the function sequence called and then views.

Resources