Laravel is throwing a 404 error on my images, css and javascript files which are located in /mysite/public. I've set /mysite/public as the site's root folder and all my assets are located in there.
I did a google search on this error but they all gave the same solution i.e {{asset('css/style.css')}}. I already have my links setup like this so I don't think this is the problem.
I think the error has something to do with my Rewrite rules but I just can't figure our what it is.
My site's .htaccess file is:
RewriteEngine On
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.my-site.com/$1 [R,L]
</IfModule>
If I delete the first RewriteRule, my css loads fine but pages other that the homepage will start throwing a 404 error. If I put it back, other page work fine but css, js and image file stop loadings.
Your first rule essentially says "regardless of what the URL is, load index.php". This means it will never load your assets, which are physical folders, as it will just load index.php instead. For example, if you tried to load css/image.png it will just load index.php.
If you remove it, your actual folders will load, but other URLs aren't re-written to index.php, which is why it will break.
You should use the .htaccess provided to you by Laravel. This should be your .htaccess file in the public/ folder (as per the Laravel Github repository)
<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>
public/.htaccess
If the public folder is not the root on your server, you'll also need to have this in an additional .htaccess (in the folder above public)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
/.htaccess
add this rule to keep assets from being routed to the router
RewriteEngine On
# Don't rewrite for css/js/img assets
RewriteRule ^assets/.* - [L]
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.my-site.com/$1 [R,L]
Related
I've built a php website working all fine with all the redirects except for http:// versions.
So I'm trying to edit my main .htaccess file to prevent http:// showing the error "NOT SECURE", and forward the user to https://
I've used a framework called TraversyMVC while building my website and it has the setup below in the public_html directory .htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
And this is my .htaccess file in the /public directory
<IfModule mod_rewrite.c>
Options -Multiviews
RewriteEngine On
RewriteBase /public
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
</IfModule>
This is what I've tried in my public_html directory .htaccess file but it didn't work:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} public/ [L]
RewriteRule ^(.*)$ https://www.mywebsite.com/public/$1 [L]
</IfModule>
Looking for the correct solution.
Thanks
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} public/ [L]
RewriteRule ^(.*)$ https://www.mywebsite.com/public/$1 [L]
You are redirecting the request before the internal rewrite to the /public subdirectory (so the second condition that checks for public/ will never match) and /public should not be in the visible URL you are redirecting to. You should also be explicit that this is an external redirect, not an internal rewrite. (As written this rule would result in an implicit 302 - temporary - redirect.)
This needs to go before the existing rules in the root .htaccess file. Try the following instead:
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]
Test first with a 302 (temporary) redirect to avoid potential caching issues.
You do not need the <IfModule> wrapper.
I have a blog folder in my servers /home/app/sites directory, and I have my laravel app in /home/app/public_html/laravel and my domain www.laravelapp.com is pointing to the project.
I want to point the url www.laravelapp.com/blog to my blog folder in /home/app/sites/blog.
How can I do that?
Here is my .htaccess file:
<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}]
# Redirect blog folder (Not working)
RewriteCond %{HTTP_HOST} ^www.laravelapp.com/blog$ [NC]
RewriteRule ^(.*)$ /home/app/sites/blog [L,R=301]
</IfModule>
../ goes one folder down
So it should be ../../sites/blog
There may be restrictions with htaccess. If this not already work share your .htaccess file. You cant go behind your base url, the following question explains it very well.
Having links relative to root?
i have this .htaccess file, but i need to want to have a 404 error page. but when i add ErrorDocument to it i do nothing. if i write localhost:8888/home/somepage-there-not-exist.
<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]
</IfModule>
it's located in the root same with the app and public folder
Ind the public folder there are this too:
<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 know the code coveret /something to files request, but when the file not exit it should get a 404 error.
It's from laravel's root .htaccess after some i like some part of the framework, but i wanna make a code block there only is there to make the website secure, and not a framework (don't wanna invent the wheel again). But i still need it to understand urls that way because it make it SEO and open so there no rules are for the URL
So i have student laravel's code and figure out how it works, but this part about 404, 403 and so on.. is so complicated in laravel's and cross so maney files that you don't have a chance to understand the code and find out how laravel know when it's a 404 error.
UPDATE
I have found this ErrorDocument-VS-RewriteRule
after what i see i can use
RewriteRule ^(.*)$ - [L,R=404]
ErrorDocument 404 /errors/errors.php
thats kinda works, but not 100%. the ErrorDocument don't work when you are 2 steps in example: localhost/step1/step2 and it also broke it so when the site exist it still show the error page. i have try to set the code over and under the RewriteRule ^(.*)/$ /$1 [L,R=301]
Yes this project is maybe stupid but i do it for lerning
On my domain public directory is the laravel public directory.so there is index.php and htaccess file. but i want to access mydomain.com/demo/, where demo is a folder.its always redirect me to my home page as mydomain.com because of route rewrite . so how can i access folders in mydomain.com. my htaccess is followed. ...
<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]
</IfModule>
thanks in advance
Create a separate .htaccess file within demo directory to override the .htaccess file in your root directory.
Order Allow,Deny
Allow from all
Create a separate .htaccess (as Brian Dillingham said) file within demo directory to override the .htaccess file in your root directory.
But put use codes below instead:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
Also, you can refer the answer from: https://stackoverflow.com/a/38068912
For the ones out there who use a public domain but the Laravel directory is within a subdirectory. You might have the following folder structure:
- / <-- directory your domain (e.g. "https://example.com") points to
- some_subfolder1/
- some_subfolder2/
- laravel_project/
- ...
- public/
- .htaccess <-- File one has to modify
- index.php
- ...
The .htaccess File's RewriteRule for Front Controllers would look the following:
RewriteRule ^ laravel_project/public/index.php [L]
So you will end up with:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteBase /
# 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 ^ laravel_project/public/index.php [L]
</IfModule>
In addition, I also modified the .env-File for the APP_URL param:
APP_URL=https://example.com/laravel_project/public
Furthermore, you might think of moving the laravel project files (despite from the public folder) outside of this file tree.
I have a blog set up at blog.ftj.com/ACSM, it is hosted with Bluehost and their folder structures seem to be case sensitive. Is there something in the .htaccess file that I can adjust so that all possible combinations get redirected to the specific uppercase URL.
Another issue is that it seems that I need to redirect
blog.ftj.com/acsm/
with and without the forward slash.
Here is my current .htaccess file
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /ACSM/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /ACSM/index.php [L]
</IfModule>
# END WordPress
Please submit the full change if you would.
You need to place the following .htaccess in the root dir to rewrite all requests to /ACSM into /acsm
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/acsm$ [NC]
RewriteRule ^(.*)$ /ACSM [L]
RewriteCond %{REQUEST_URI} ^/acsm/(.*)$ [NC]
RewriteRule ^acsm/(.*)$ /ACSM/$1 [L]
</IfModule>
Sorry for delays, have not got an Apache at hands....