简体   繁体   中英

How to modify htaccess file to load properly my assets resources using Laravel 5.7?

I'm working with Laravel 5.7.

I have modified the .htaccess file to eliminate the public path of my application.

The problem is that now only assets are loaded from the pages that have a one level of path eg( http://localhost/myapp/home ) and not in those with more than one level eg( http://localhost/myapp/home/example/example2 ).

I tried to use laravel's own functions like asset() , URL::asset() and also the HTML::style() helper, but none of them correctly loaded the assets.

Also, I have used several examples of .htacces files from previous projects and they have not worked either.

This is the code of my .htaccess file, that only load one level routing assets

Options -MultiViews -Indexes

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_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$[NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [L,NC]

I would be grateful if any of you had this problem and has been able to solve it, could you give me a guide or help.

Greetings colleagues.

You should not change any directory structure inside laravel or your .htaccess file. The way you should do it is to make a symbolic link between your public folder of the laravel project and the public_html folder in your server. Revert all changes you made, and just link the two folders like usual. For example:

ln -s ~/nameOfProject/public public_html

This way you keep your laravel project as it should be, and all functionality will remain the same. plus you get the benefit of working with git.

PS

You will need SSH access, and yes this should work perfectly fine on "shared hosting server".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM