NicolasGoosen's avatar

Not seeing .css file

Am following the PHP Practitioner series - on the "Dry up your Views" video.

Why do 'require' statements work, but my (to css) statements won't work. It seems to be the routing that's interfering - it can't fetch the .css file because it's not in the routes.php file, I'm guessing.

If this is the case, would that have something to do with PHP being 'compiled' on the server whereas the html tag would only be processed once already in the browser?

For the purposes on this course I'm on MAMP with the following .htaccess file in my htdocs folder:

RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME}% !-d RewriteCond %{REQUEST_FILENAME}% !-f RewriteRule ^(.+)$ index.php [QSA,L]

PS, I don't know how to write .htaccess files - just copied this one from someone else.

0 likes
10 replies
Tray2's avatar

Without any code it's hard to help you.

NicolasGoosen's avatar

Thanks for the reply. My code is exactly as per PHP Practitioner project up until video 18. Everything is working perfectly except HTML can't pull in a .css file. The tag just won't fetch any sort of file from anywhere.

What's different is I'm on MAMP and that's my .htaccess file (and I'm not sure how .htaccess files work). My thinking is that the statements in this file are preventing my HTML from fetching anything (like .css files).

NicolasGoosen's avatar

Not a huge concern for me right now, but my motivation for wanting to know is, I'd like some more info on this regarding setting up an actual Apache server to run a Laravel project. If we're redirecting all requests to public/index.php so our router can do its work then how is the HTML going to fetch .css or .js files?

Tray2's avatar

Your css is probably placed in a folder in your document root something like

./public/css or similar so in your html header you just reference that like you do in a static html page.

<link href="/public/css/mystyles.css">

NicolasGoosen's avatar

It's not my file path that's wrong. If I remove the .htaccess file then suddenly the css works. With .htaccess in there, no css.

NicolasGoosen's avatar

...ps to last post...

...which seems logical to me - the purpose of the .htaccess is to redirect everything to index.php - even the browser's requests for css files?

But, then, how to get around this?

NicolasGoosen's avatar

Thanks, Tray2. I think it's starting to get over my head now - I'll have a look at that. In real-world scenarios, in an Apache / Laravel / Homestead setup - how would one go about setting up this sort of thing. It must be a common scenario??

Snapey's avatar

modrewrite does not rewrite everything. It only rewrites requests for which it cannot find a physical file or folder that matches the request.

So if attempts to load your css files end upbeing rewritten to index.php then its because the file is not at the url you specified

Please or to participate in this conversation.