Laravel make routing to subfolder on deploy - php

I have a Laravel Apps and a shared hosting. I wanted to deploy my Laravel apps to cpanel, and got an error
404 resources not found when deployed. I have put the laravel public folder into the root folder, and the laravel file into subfolder /laravelApp.
So my hosting structured like this :
bdd.services
|--> public_html
|----> myLaravelApp
|------> css
|------> js
|------> laravelApp
|--------> app
|--------> /* and all the laravel apps inside here */
|------> .htaccess
|------> index.php
|
|
|----> someonesProject
When I tried to test the app, I see the request was requesting to
https://mydoamin.com/login/auth where it should be https://mydoamin.com/myLaravelApp/login/auth. And it shows an error 404 resources not found.
So how to make the routing always go to https://mydoamin.com/myLaravelApp/? or maybe there's another solution?
here's my .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_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've also set the APP_URL at the .env.
APP_URL=https://mydoamin.com/myLaravelApp/

In this case you should ensure that you have
APP_URL="http://example.com/folder"
In your .env file
Then instead of doing an href like this
href="/login"
Give your route a name
Route::get('login', ...)->name('login');
and do your href like this
href="{{ route('login') }}"
This way it will always use the full url

Related

Laravel 8.4 – Remove Public from URL

I am working on creating a website that converts text into speech. I am using the script from codecanyon which is Developed with PHP 7.4.x and Laravel 8.4.x . The issue I am facing is that I don't want to show /public URL as a permalink to access the website. Now, if I type mywebsite.com/pubic then the website is accessible otherwise if I write mywebsite.com then it only shows the files. I followed some of the previous queries posted on StackOverflow but got no results.
The topic I followed is Laravel 5 – Remove Public from URL
I tried even by renaming server.php , copying .htaccess and rewriting it. But those all resulted in a screen showing up errors or even blank.
My .htaccess file 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]
To remove the /public from the URL you are not going to use the .htaccess file.
What you are going to do instead is set the /public folder as the serving directory for your Server.
If you use Apache, just go to your /etc/apache2/sites-available/000-default.conf file and change the DocumentRoot from {your base folder}/ (usually /var/www) to {your base folder}/public
The content of public folder should go to the public folder of your server (fe it's public_html on my server), and everything else should be stored in a different folder (fe laravel_mywebsite). Then you should go to public_html/index.php and change the folder paths based on where you put your Laravel app.
If you want to only use .htacces, you just need to rewrite all requests from /public to /
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,R=301]

Laravel 8 Cpanel Deploy - 404 Errors

I am trying to deploy my Laravel 8 app to a folder in CPANEL (first time I've deployed Laravel). I have searched the forum and made changes as the many posts direct. I am getting 404 errors across all pages apart from the homepage and login and register pages. This is a common error but I cant find a solution. Dont shoot me.
1.All files uploaded to url/laravel/blog
Public folder moved outside and renamed public_html
The /laravel folder now has two directories /blog and /public_html
index.php in the public_html folder updated:
require DIR.'/../blog/vendor/autoload.php';
$app = require_once DIR.'/../blog/bootstrap/app.php';
New .htaccess file added to the root of the public_html and set to chmod 755 (detailed below)
Renamed the cache folder in the /bootstrap folder so that new cache is created (I hope)
Database is linked within the .env file
I am out of ideas, I have created a test html file in the public folder and I can see this in the browser when I navigate to it. I am learning and any help will be appreciated.
<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 ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
I needed to point the subdomain/add-on domain to the public folder. No need to move anything or change Htaccess etc.... Credit to https://stackoverflow.com/users/2325620/james-clark-developer
Try this in your .htaccess
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^$ public/index.php [L]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]
</IfModule>

Redirect Url Issue with Laravel and htaccess

I create virtual host for my project in local xampp. Url: http://kooltech-us.com/
I have folder's name admin (From picture below ) in public folder (Laravel) .
in my project my admin login url http://localhost/admin/login .
I hit url http://localhost/admin/login to go to admin login page .. it's okay..
but when i hit this http://localhost/admin . it takes me to the public/admin folder . That means show me public Directory listing.
For prevent access Directory listing or public/admin I write in Options - Indexes code in .htaccess file .it's give me 403 Error ( Access forbidden! ) in general. it's work.
I looking for that when I hit the url http://localhost/admin it will take me to http://localhost/admin/login .
Note:
I did not change folder structure. (Laravel Default Folder Structure Exist).
Only one line code I write in .htacees file : Options - Indexes . that give me 403 Error ( Access forbidden! ) in general.
Version "laravel/framework": "5.8.*"
I write in web.php but it's not working ..
Route::get('/admin', function () {
return redirect('/admin/login');
});
if need more explanation please comment..
To modify the .htaccess you would need to add something like this to your .htaccess This will redirect all request from /admin to /public but still show as /admin which will allow your redirect in web.php to work:-
RewriteRule admin /public [L]
Which would give you:-
<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 admin /public [L]
RewriteRule ^ index.php [L]
</IfModule>
However I still feel that this is bad practice, and changing your directory strcture as #flakerimi sugested would be a much better solution.

Laravel Project in subfolder reroutes to main domain. Is the setup right and how to add prefixes?

For the testing of a new version of a project, I have to deploy a laravel project to a sub folder which was set up for me. I already programmed it on the localhost and only need to upload it.
The subdomain is on example.com/new
The structure I found in Filezilla was like this:
(I do not have access/can't see to the main Versions files-does this mean it is a subdomain?)
htacess
env
newProject(folder)
public
I did not have permission to upload new files above the public folder. The .env and .htacess could be replaced. So I did the following:
htacess
env
newProject(folder)
public
index
project(folder)
(laravel put another.env and .htacess in here are these the right ones to change)
.htacess
.env
rest of laravel files
Out of confusion I always just make a change in both existing .env's and .htacess because I don't know which is the right one. Is this setup right? I already changed the index
require __DIR__.'/project/vendor/autoload.php';
$app = require_once __DIR__.'/project/bootstrap/app.php';
My htacess:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
[[rewritebase /new
# 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>
MY PROBLEM:
There is at least something working. Because I have an automatic rerouting for languages in the web.php
Route::redirect('/', '/en');
Route::group(['prefix' => '{language}', ''], function (){
...routes
And when I go to example.com/new it reroutes me to example.com/en instead of example.com/new/en. That means it does come to the routing part, dies this mean htacess and .env are set up right?
the error is of course
"Not Found
The requested URL was not found on this server."
MY QUESTION
How do I stop the rerouting to /en and go to /new/en. Is there a prefix I can add to all my routes. And can I be sure the rest is set up right just because the code routes to /en which is set up in the web.php?

laravel 5.5 - route to folder

I upload all my laravel 5.5 app in to my shared server (www.example.com/laravel/) folder. now when I goto www.example.com/laravel it will launch my app. but I have 2 route issues:
my route in web.php is not working anymore. when I click:
AboutUs
it will go to www.example.com/laravel/about, and the result is understandably Not Found because in my web.php my route is:
Route::get('/about', 'HomeController#about')->name('about');
I even change to
Route::get('laravel/about', 'HomeController#about')->name('about');
and it still not working. How can I resolve this?
I still have my public folder inside the /laravel/ folder. To make all the css and js file to link, I have to change from:
to
<link href="{{ asset('public/css/app.css') }}" rel="stylesheet">
to make it to work. Is there away to reconfigure this?
I can hardcode both to make them work. But that will heavily affect my constant develop and update process. I am looking for a way to set it once and no need to change between development and production.
Update01
I tried #ben's suggestion and change the .htaccess in the 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
RewriteBase /laravel //<-----this is what I add!
RewriteRule ^ index.php [L]
</IfModule>
but nothing seems to work. I am very new to .htaccess so I am not sure of what I am doing there. Any more insight?

Categories