I'm working in my first project with Laravel, it's a simple website with an admin panel:
In my "public" folder I have a directory called "admin" where I put all the styles and scripts corresponding to the admin panel. I've also defined a route in my app to handle the "admin" GET:
Route::get('/admin', 'Admin\DashboardController#index');
The problem is that since I have that "admin" folder in my public directory Laravel is ignoring the "admin" route I defined, so I can't access the proper controller. I'm suspecting it has something to do with the .htaccess but I'm not sure how to solve it. This is how my htaccess looks right now:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
The issue is with the following line:
RewriteCond %{REQUEST_FILENAME} !-d
In your example this tells the web server not to send the /admin route to Laravel because it is a physical directory. If you remove that then the /admin route should work. This will however prevent browsing directly to a folder and getting a directory listing, which should not be a problem. The next line in the htaccess will allow you to link to asset files contained in your admin directory and not have them get processed by Laravel, so that should not be removed.
The newer version of Laravel also contains:
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
It should be updated to the following in order to avoid a redirect loop on the admin route:
# Redirect Trailing Slashes...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
So if using the newest version of Laravel, your htaccess file should look something like this:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
You cannot have a public directory with the same name as your route - otherwise how will Laravel know whether "/admin" is for the controller, or for the style etc.
You should store your admin style sheets etc under /assets/admin/*
For routes.php:
Route::controller('admin/dashboard', 'Admin\DashboardController');
For using assets in your public container you can now use:
<script type="text/javascript" src="{{Request::root();}}/assets/js/jquery.js"></script>
Where assets in the /public/assets/js/jquery.js
Hope this helps.
Related
I am about to deploy a laravel project. I uploaded all the files and the website is working fine but the only problem is when i try to access my website i.e for example if i try to write mywebsitename.com in the url then public directory is shown which is because i have symblinked my laravel's public folder to the public_html folder. I don't have any idea about how to edit the .htaccess file in order to load the website with mywebsitename.com and not mywebsitename.com/public. My htacess files code 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]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
my file directory structure is shown in this image
please help me
I am guessing it is an apache server. If yes;
Ensure your domain mywebsitename.com points to the public_html
Add a .htaccess file in your root directory(public_html) and paste this in
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
I found a solution to this question.
1.Copy all the laravel files inside the public_html folder including the .htaccess file and the index.php file which is suppose to be inside the public folder(keep both the files inside the public folder).
2.Then link your storage to the public folder using SSH, for that open your SSH enter id and password and change directory using "cd domains/yourdomainname.com/public_html"
then give the php artisan command i.e "php artisan storage:link"
(note:- after changing the directory you can give all the php artisan commands like migrate and config:cache or clear:cache as you wish.
3.Now after the all the necessary commands are given you can access your fully functional website including the image display function if you type yourdomainname.com/public on the URL but if you type just your domain name then all the laravel files will appear on the browser to avoid this and to to jump inside the public folder directly create one more .htaccess file outside the public folder but inside the
public_html folder the copy the below given code and then if you just your domain name on the URL then your website will be directly displayed
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteRule ^ public [L]
You can use it with RewriteBase directive,
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteBase /public_html/ # or '/' only depends on your vhost config
# 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>
I'm not familiar with apache server but the problem should be in the default index file.
For long terms considered, I advise you to use nginx or openresty, it's easier to configurate.
I have two project folder, e.g. blog1 and blog2.
/var/www/html/blog1
/var/www/html/blog2/laravel
I am doing it as sub folder instead of sub domain.
blog1 folder consists of laravel project. Inside blog2 folder, there's is a folder name laravel which consist of laravel project.
this is my htaccess for blog1
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteRule ^(.*)$ blog1/public/ [L]
</IfModule>
Every time access too blog1 is simple, just https://website.com/login
Every time i want to access blog2 project, this is the link looks like https://website.com/blog2/login
htaccess inside blog2
<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 ^(.*)/$ / [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>
How can i make the link looks like https://website.com/blog2/ or https://website.com/blog2-sg/ or https://website.com/blog2-uk/?
My expected result
https://website.com/blog2/ will go to laravel login page
https://website.com/blog2-uk/ will also go to laravel login page
They will route to /var/www/html/blog2/laravel
Can i know is this doable and how to achieve it? Thanks.
I suggest looking in to Laravel Routes Subdomain Routing example:
https://laravel.com/docs/6.x/routing#route-group-sub-domain-routing.
This demonstrates how to use variable/dynamic parts in the URI.
Point all /blog2-*/ URIs to the Laravel application and let Laravel do further routing. I don't know your exact situation, but maybe the following redirect rule could help you out:
RewriteRule ^blog2-(.*)/ http://www.website.com/blog2 [R=301,L]
I have a Laravel 5.7 project and I can't seem to understand why I get a 404 error to the subfolders in the public directory.
Directory structure
public
icons
css
file.css
Example of routes that work just fine:
localhost/images/image.png
localhost/css/main.css
However when i try to request a file deeper that the first subdirectory, i get a 404. For exaple:
localhost/pugins/common/common.min.js
file located in public/pugins/common/common.min.js
localhost/icons/weather-icons/css/weather-icons.min.css
file located in public/icons/weather-icons/css/weather-icons.min.css
Anything after localhost/FOLDER/ gets a 404 eror, even if it exists.
Any ides why?
This is the default .htaccess file:
<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>
I have noticed just now that the first level of the subdirectories of the public folder get a 403 (Forbidden) error while try to list that directory.
Example:
localhost/js => 403, Forbidden
localhost/js/dashboard/intro-inde.js => 404, Not found
localhost/js/script.js => 200, Working fine
Each file and directory exists in the examples above, and there is no problem with the permissions.
My guess is that it has something to do with the .htaccess, but i have no idea how that works >.>
I remembered i had no problems in previous projects using laravel 5.1 ~ 5.5. So I have copied the .htaccess file from those projects to this one. Now everything works fine, but for one folder and its subfolders : public/icons; I solved this problem by creating a new folder and moving everything from "icons" to "fonts-icons"
Behold the power of the new .htaccess. Maybe it will help someone.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# 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]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
This one solved the problem...without the new for new routes. :D I still have no idea how it works and why, though.
Just for curiosity, maybe someone will enlighten me d=(^_^)=b
You should first define that route in the routes/web.php file.
Error 404 is when we do nnot find any pages. Which means we have to define route in our web.php file first so as to view desired content written in the blade.php file, This error may also appear if we have not saved the file in our views in .blade.php extension
I'm using a shared hosting server, I installed my first Laravel 5.4 application and everything is still working fine. A few days ago I installed another Laravel 5.4 application on a different domain, but same server, but when I try to access the URL, I get 500 INTERNAL SERVER ERROR.
Below is my .htaccess code
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
I also tried the following, but same error:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
### Add two lines for fix error 500 ###
Options -Indexes
php_flag xcache.cacher 0
#######################################
# 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>
You moved everything inside your public_html folder which you should not do. Only files inside /public needs to be in public_html, hence the name public. The Laravel files should be placed higher directory where it is not accessible from the public.
Install Laravel with Softaculous, or with composer via SSH in /home/username/laravel
Move everything in /home/username/public_html folder to
/home/username/laravel
Move everything in /home/username/laravel/public to /home/username/public_html and delete the public folder after move.
Edit this file: /home/username/public_html/index.php
Change to require '/home/username/laravel/vendor/autoload.php';
Change to $app = require_once '/home/username/laravel/bootstrap/app.php';
Save this file and close window.
Edit this file: /home/username/laravel/server.php
Change to:
if ($uri !== '/' && file_exists('/home/username/public_html'.$uri)) {
return false;
}
require_once '/home/username/public_html/index.php';
Replace username with your hosting username. :)
I just found the problem on server. all is working fine in my localhost, but on live server, ONLY the home page route is working.
My directory is:
laravel-
css
js
local->
app
HTTP->
Controllers->
Homecontroller
admin->
Groupcontroller
config
...
Here is my htacess
<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
RewriteRule ^ /index.php [L]
RewriteCond %{HTTP:Authorization} ^(.+)$
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
And my route file is:-
Route::get('/group/detail', 'Groupcontroller#index');
Route::get('/group/add', 'Groupcontroller#create');
Route::get('/group/edit/{id}', 'Groupcontroller#edit');
http://www.example.com/home
My home controller is working.I think issue is with admin folder???
http://www.example.com/admin/group/detail
This is not working
Error is encountered :-
Class App\Http\Controllers\Admin\Groupcontroller does not exist
Please help me,Working fine at localhost but not on live.
Thanks in advance
Try and change the content of your .htacess to this
<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>
Might be you forget to paste .htaccess file from /public folder to /public_html folder
so Just COPY And PASTE the .htaccess file from public folder to /public_html folder
then it will work as local
Path of Groupcontroller is Controllers/admin/Groupcontroller. So in your routes you need to access Groupcontroller with appropriate path.
Route::get('/group/detail', 'admin\Groupcontroller#index');
Route::get('/group/add', 'admin\Groupcontroller#create');
Route::get('/group/edit/{id}', 'admin\Groupcontroller#edit');
Also it is recommended to use CamelCase folder names. that is; change admin => Admin.
Check the namespaces compared to directory names. The cases should match.
You can try the bellow code :
After the domain name add "public" keyword between your API routes
for example:-
https://example.com/public/api/someroute
After this, you may have to update your PHP version .