Laravel 5.2: asset path not working on server - .htaccess

My laravel 5.2 app unable to fetch the stylesheet from the public/css directory on the production server but can fetch stylesheets on the local machine!
here is how I am linking my files:
<link href="{{asset('css/clock.css')}}" rel="stylesheet" media="all" />
I also tried bunch of different ways:
<link href="{{URL::asset('css/clock.css')}}" rel="stylesheet" media="all" />
OR:
{{HTML::style('css/clock.css')}}
but nothing worked on server!
but it is not able to fetch the css! when I check the page source, it shows me some other css file linked.. is it because of '.htaccess' file that I used to redirect to the public folder of my site?
you can have a look at enter link description here..
please help!

Why are u not trying url helper like this
<link href="{{ url('css/clock.css')}}" rel="stylesheet" media="all" />
Note:- css/clock.css is in 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]
# Force SSL
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [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}]

The asset() helper prepends the base URL to the path you supply.
But laravel keep js, css, and image files into public folder so you need to add public path like this:
href="public/{{ asset('css/style.css') }}.
Instead of doing this edit into this file:- vendor\laravel\framework\src\Illuminate\Foundation\helpers.php
function asset($path, $secure = null)
{
if($_SERVER['HTTP_HOST']=='127.0.0.1:8000')
return app('url')->asset($path, $secure);
else
return app('url')->asset('public/'.$path, $secure);
}

Related

htaccess rewrites my css and js, even source wont change

I am trying to learn VueJS and Twig. I made my own (just for fun) MVC and that is working fine, the problem lies in not being able to include JS or CSS files.
When I press CTRL+U and view my source, I click on my javascript file and then the page reloads with the correct URL but I will still see my index pagina...
I have no clue what is causing this and -or how to fix this. Does anyone have any ideas on how to fix this?
root
htaccess
public
htaccess
My root htaccess file is:
<IfModule mod_rewrite.c>
Options -Multiviews
RewriteEngine On
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
and the htaccess file in my public folder is
<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>
My layout (index) page is this:
<!DOCTYPE html>
<html>
<head>
<title>Beta</title>
<link rel="stylesheet" href="../../public/css/style.css" />
<script src="../../node_modules/vue/dist/vue.min.js"></script>
<script src="../../public/js/scripts.js"></script>
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>
As said, when I click on my CSS or JS file I see this in the source:
<!DOCTYPE html>
<html>
<head>
<title>Beta</title>
<link rel="stylesheet" href="../../public/css/style.css" />
<script src="../../node_modules/vue/dist/vue.min.js"></script>
<script src="../../public/js/scripts.js"></script>
</head>
<body>
Hello there!
</body>
</html>
Hope someone can help me out!
UPDATE:
I changed my htaccess to this:
<IfModule mod_rewrite.c>
Options -Multiviews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?/$1 [L,NC]
</IfModule>
And now my CSS and JS are visible, but the node_modules (Vue) still shows this weird behavior of just showing the index page's source code.
Obvious rookie mistake!
You cannot include something from the "node_modules" folder. I used GULP to include and compile everything to one of my publically accessible folders and now I can include them perfectly fine!

htaccess - Run url if correct rewrite

I try to rewrite rule url in .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^myfolder/([^/]*)$ /myfolder/index.php?link=$1 [L]
RewriteRule ^myfolder/([^/]*)/([^/]*)/([^/]*)$ /myfolder/functions.php?model=$1&id=$2&method=$3 [L]
So, it works. But, my css is not loading. I tried to find some solution and when i change:
<link href="css/colors/blue.css" rel="stylesheet">
to
<link href="css/blue.css" rel="stylesheet">
and remove blue.css from "css/colors" folder and add it to "css" folder. My css is loaded.
My question is How to load css in "css/colors/blue.css".

Htaccess file - 404 page not found on some folders

I am using codeigniter and I keep having these 404 errors in my log file.
ERROR - 2016-04-30 16:41:15 --> 404 Page Not Found: Assets/js
ERROR - 2016-04-30 16:41:15 --> 404 Page Not Found: Assets/js
ERROR - 2016-04-30 16:41:18 --> 404 Page Not Found: Assets/js
So every page load causes one entry of the above error in the log file. My htaccess file looks like below. What's going wrong here?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond $1 !^(index\.php|assets|myadmin|robots\.txt)
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
I can confirm assets/js are valid folders.
Are you able to get to your assets folder by going through the web path? e.g. http://localhost/assets (try to retrieve a file if you dont have indexing enabled in your webserver e.g. http://localhost/assets/css/style.css), does it load?
If this is loading you are possibly calling your assets without using a base path which means a view is trying to call directly from within its own folder.
Using site_url() is a really good step (url helper)... I urge you to make your life easier in calling assets by making a simple helper and adding it to auto load containing (something like) this:
if (!function_exists('assets'))
{
/**
* Base asset directory call
*/
function assets($var)
{
$CI =& get_instance();
$CI->load->helper('url');
return site_url('assets') . '/' . $var;
}
}
You would then call it in your view:
<!-- load CSS -->
<link href="<?php echo assets("css/bootstrap.min.css") ?>" rel="stylesheet">
It would then show this once rendered:
<link href="http://localhost/assets/css/bootstrap.min.css" rel="stylesheet">
This is just a really basic example though, if you have heaps of assets to call in a header or footer, I would suggest creating a helper to load based on a config file, you can also specify what is required on certain page loads that way as well so its dynamically loaded with a foreach.
Also do be careful calling Assets (capital letter) vs assets (no capital letter) is very different, many webservers are case-sensitive...
I run the following for my htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond $1 !^(index\.php|assets|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

Hide folder name from address bar but only 1 internal css page link not work

I want to hide a folder name from Address bar by .htaccess, that means hide myfolder name from: http://www.myweb.com/myfolder/mypage but only 1 internal css page link not work.
I used the below code collected from stockoverflow, Its hide folder name well but a internal page links not work.
Here others css and js link work well also.
Information: I have 2 css folder. One is root directory and others one
is myfolder directory.
Internal page link:
All css and js page link like as
<link href="css/style.css" rel="stylesheet" type="text/css" />
Try 1:
<link href="../myfolder/css/style.css" rel="stylesheet" type="text/css" />
Also try 2:
<link href="http://www.myweb.com/myfolder/css/style.css" rel="stylesheet" type="text/css" />
my htaccess
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+myfolder/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^myfolder/)^(.*)$ /myfolder/$1 [L,NC]
The problem is that the following rule only passes the request to files inside /myfolder if the request path does not already exist. But, since a /css/style.css also exists at the root, the test fails and preempts the rule.
RewriteCond %{REQUEST_FILENAME} !-f # if not a file
RewriteRule (?!^myfolder/)^(.*)$ /myfolder/$1 [L,NC]
The easiest way to resolve this would be to either rename the folder or css file inside the myfolder directory. The other way would be to append a query string tag like
<link href="css/style.css?myfolder" rel="stylesheet" type="text/css" />
and check for it within the .htaccess like
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{QUERY_STRING} ^myfolder$ [NC]
RewriteRule (?!^myfolder/)^(.*)$ /myfolder/$1 [L,NC]

CodeIgniter Assets Not Found

Please help with CodeIgniter assets. I tried most of the solutions from StackOverflow (including the autoload utility helper), nothing works so far. Here's my setup below:
Folder Path:
.htaccess
<IfModule mod_rewrite.cs>
RewriteBase /hnk_ci
RewriteEngine On
RewriteCond $1 !^(index\.php|assets|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
When I view source code, it looks like this:
<link href="http://localhost/hnk_ci/assets/css/bootstrap.min.css" rel="stylesheet">
But when click on the css link from source code, I get this error:
The requested URL /hnk_ci/assets/css/bootstrap.min.css was not found on this server.

Resources