laravel 5.5 - route to folder - php

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?

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 make routing to subfolder on deploy

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

Flush cache in Laravel 5.2 on shared hosting without SSH?

2 questions actually:
How do I setup a Laravel site in a subfolder?
How do I flush/clear the cache?
My current situation:
I'm on shared hosting (don't mock me....I know dedicated server is better....trying to figure out how much budget to allocate for it so currently using shared since I can pay monthly) and can't use SSH.
URL structure is like this: website.com/v2 where "v2" is the subfolder where the site is installed on.
Is this the way my .htaccess and index.php are supposed to be setup?
.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteBase /v2/
# 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}]
index.php
require __DIR__.'/../v2/bootstrap/autoload.php';
$app = require_once __DIR__.'/../v2/bootstrap/app.php';
For clearing the cache, I got this code in routes.php:
Route::get('/v2/configcache', function() {
$exitCode = Artisan::call('config:cache');
print_r($exitCode);
});
Are all of the codes above setup correctly? When I go to mywebsite.com/v2/configcache, I get an "Under Construction" message.
I need to clear the cache because I updated the database info.

What is the best way to load only 1 page over https in laravel?

In my laravel application I need one page to be over https because I want the user to use his microphone.
The method getUserMedia and is currently only allowed over https https://sites.google.com/a/chromium.org/dev/Home/chromium-security/deprecating-powerful-features-on-insecure-origins
For performance I only want this page that does audio recording to be over https. But that also means that I need to load all my assets over https with 'secure_asset' in this page only. That means I will have something like this in my master blade for all my assets:
#if(Request::is('record'))
<script type="text/javascript" src="{{secure_asset('/js/jquery.js')}}"></script>
#else
<script type="text/javascript" src="{{asset('/js/jquery.js')}}"></script>
#endif
What is the best and cleanest way to achieve this with laravel routing?
Use 'https' => true] in your routes.php just for this one route/page - that seems very clean to me.
Example:
Route::post('/yourroute', ['uses' => 'YourController#method', 'https' => true]);
Update
You can also use the .htaccess file in your public folder.
<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]
# Redirect specific route to HTTPS
# The rule is looking for the content between ^ and $ and if found in the URL it redirects to https://www.example.com/yourroute
RewriteRule ^yourroute$ https://www.example.com/yourroute [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>
And yes, for your assets you need to have an if-else statement as you already using. You can either use secure_asset() asset(..., true) helper.

Run multiple subfolder websites in one separate folder

I have a main website (example.com). I use CodeIgniter framework there.
I will also use some different subfolders like example.com/sub1, example.com/sub2. I am using Laravel framework here and those will be completely different from example.com and stored in a different folder. When I started first subdomain, I used Alias in Apache2 configuration and it was working perfectly well. However I now need to use a second subfolder. I added a second Alias sub2 in Apache configuration that points to the same directory as sub1. Then I changed my .htaccess to this:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(img|js|css)
RewriteCond %{REQUEST_URI} (/evim)
RewriteRule ^ /evim/index.php [L]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(img|js|css)
RewriteCond %{REQUEST_URI} (/crm2)
RewriteRule ^ /crm2/index.php [L]
</IfModule>
Now it works fine. Both example.com/sub1 and example.com/sub2 runs the same application. However in Laravel, it is hard to find which subdomain is running currently. In routes, the application does not see subfolder name. For example if I use users/login route, then it matches both example.com/sub1/users/login and example.com/sub2/users/login. I need to define routes with subfolder name.
I am looking for a solution in htaccess or Laravel to fix the routes.
I would use this pattern (as sub domains) and it may perhaps avoid problems.
http://site1.foo.com http://site2.foo.com
In CodeIgniter Route.php you can do the following:
$server_link = explode(".", $_SERVER["HTTP_HOST"]);
if ( $server_link[0] == "site1" )
{
$route['default_controller'] = 'home';
// Some Other Routes
}
I think you can do the same thing for Laravel routes.
I used Request::server('HTTP_HOST') to get which site is loaded.

Categories