Laravel site accessible on /public/index.php/ - php

As the title suggests, I set up a server with a Laravel project (only this project will run on the server).
The server is an Ubuntu Server 22, with php7.4, mariadb and apache2.
I configured the project inside /var/www/html, added the virtual host on apache2 and tested the various services and everything works fine.
But when I reach the site via browser all the routes are under the url http://site_IP/public/index.php /...routes....
I then configured a .htaccess to make the site accessible under the url http://site_IP/...routes... but this doesn't happen and I keep accessing it under /public/index.php.
Do you have any suggestions or advice?
.htaccess
<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]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

you can try this two methods.
method 01
remove htaccess from public directory and add it in outside public directory. then after add this line to your env file
ASSET_URL=https://your_project_url/public
method 02
upload all project files to www directory and symlink your project into html directory

Related

How to deploy React laravel vite website on Hostinger?

I've spent the whole last two days trying to deploy my Laravel React Vite website on Hostinger
I followed The instructions from Hostinger docs as below:
I uploaded my website.
created new .env and add my configs.
created a database.
added build folder from react vite after running "npm run build" to the public folder.
edit resource/view/welcome.blade.php with the build js and css src as below #vite(['public/build/assets/app.a0a7c860.js', 'public/build/assets/app.69041b03.css']).
I have already .htaccess file in public folder which is
<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]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
it still gives me 403 error Although it works correctly on localhost
I tried to add new .htaccess in the project directory
as below
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
now it gives me 500 server error
I am in doubt that hostinger couldn't recognize my project structure
How to solve that problem?

Laravel Lighthouse-PHP GraphQL (Server cannot be reached)

I'm using Laravel - Lighthouse-PHP GraphQL server, and when I deploy it to production, I have to write public in the URL in order to work:
https://example.com/graphql (Server cannot be reached) ✗
https://example.com/public/graphql (Server connected) ✓
My .htaccess contains the following:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,QSA]
while inside the public folder, there's another .htaccess file, contains the below:
<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]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
I tried editing(or removing) this file, but the whole website stopped working.
Any idea what I'm doing wrong?
In your webserver configuration make sure the root directory (the directory that is serving requests) is set tot the public folder instead of the root directory of your app as you have now.
You should only make the public folder publicly accessible, not doing so could result in an attacker to access or execute files they shouldn't or strange errors/behaviour (like what you are experiencing).
I also believe this will also fix the issues you are having since the public folder is not being rewritten which is probably picked up by the Laravel router for some reason.
I don't believe the issue is with Lighthouse but with how you are hosting your Laravel application, try and see if that can be fixed and if the problem persists.

Install WordPress inside Laravel public folder

I am trying to install WordPress into Laravel public folder. I have completed the installation, but when I am trying to access the same with URL getting WordPress home page without any design.
Please find the .htaccess files of Laravel
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
#Exclude url with quora
RewriteCond $1 !^(quora)
# 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>
WordPress .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /quora
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /quora/index.php [L]
</IfModule>
Dont put your wordpress folder inside laravel public's folder, it's a wrong way to do it and can cause problem.
I advise you instead to create 2 folders on the server root, one with laravel and one with wordpress.
Then to access to wordpress from laravel you have two choice,
use a subdomain (wordpress.exemple.com)
create an alias (exemple.com/wordpress)
I think what your looking for is the alias so
to create the alias, create a new apache conf but instead of using server property, use alias property
Alias /wordpress /var/www/wordpress/
<Directory "/var/www/wordpress/">
//here your directory conf
</Directory>
enable the conf in apache, restart the server and now, on laravel when you go to the route /wordpress you're on wordpress.

.htaccess for a Laravel app installed in a subfolder

I've a domain, like www.example.com
The main site is on FTP space visible as /example.com
I installed (copy/pasted, sigh... :( ) my Laravel app under /example.com/laravel_app.
I am able to open the laravel app, and it's fully working, if I access using full url http://www.example.com/laravel_app/public/index.php.
The problem is that, obviously, I'd like to access it using only the path http://www.example.com/laravel_app.
In the root of laravel_app, I tried to paste this .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
While in the /example.com/laravel_app/public i'm using this `.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteBase /laravel_app/
# 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>
So my question is NOT related the .htaccess of app's public folder, but the .htaccess for app's root folder.
How can I succesfully serve
http://www.example.com/laravel_app/public/index.php
accessing
http://www.example.com/laravel_app
?
Actually I got Laravel returning a 404
I tried this variant (seen I some question very similar to this one)
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ public/index.php/$1 [L]
</IfModule>
But with this I got apache returning a forbidden
I'm assuming you have installed your application on shared hosting and ssince it's running Apache this solution should work.
Revert your .htaccess back to its original/what was deployed with laravel.
Ideally you shouldn't have your project uploaded in public_html as it exposes all the files to the web.
Move all your project files except public to a directory below public_html.
Eg.
/home/<username>
[laravel] <-- Make this directory
[public_ftp]
[public_html]
[ssl]
etc..
Then in your public_html move the files inside your public folder to the directory you want to serve your files (address you want to go to to use your app).
/home/<username>/public_html/laravel-app
[public_html]
[laravel-app]
-index.html
-robots.php
-.htaccess
-[css]
-[js]
-[svg]
etc..
Now edit your index.html file in the laravel-app folder and change both:
require __DIR__.'/../vendor/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php';
..to reflect the path where you moved yoru project folder.
require __DIR__.'/../../laravel/vendor/autoload.php';
$app = require_once __DIR__.'/../../laravel/bootstrap/app.php';
Essentially in this context ../ means back one folder, so the aim is to take it back to where it will find the laravel/vendor/ folder, vice-versa.
Realistically that should sort your issue out and make for a more secure application as your .env and composer, etc. files will not be publically available without messing with your .htaccess.
Oh, also here is the default .htaccess code which should be under your public folder <project>/public:
<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>

laravel: remove folder directory in url shared hosting

I just recently deploy my Laravel app and I want to remove the folder directory. Example:
example.com/laravel/anotherfolder/home
I want to remove the laravel because that is the folder
I have tried searching on google but they don't work, here is my .htaccess
DirectoryIndex public/index.php
<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 ^laravel/(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ public/index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>`
Depending on your shared host provider, you need go on your control panel and change the public directory to laravel/public.
Another solution would be to replace the current public folder by a symlink which links to laravel/public. You'll need an SSH access for this solution.

Categories