htaccess single page entry not serving static files

Viewed 66

I'm creating a single page entry point in PHP. The routes work just fine but static files such as Js & CSS are returning 404. I don't know if this has to do with the htaccess redirect.

Here's the htaccess file's code:

RewriteEngine on
RewriteCond ${REQUEST_URI} !\.(?:css|png|js|jpe?g|gif)$ [NC]
RewriteRule ^([a-zA-Z0-9\-\_\/]*)$ index.php?p=$1
RewriteCond ${REQUEST_FILENAME} !-f
RewriteCond ${REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?p=$1 [L,QSA]
3 Answers

Try without first RewriteRule:

RewriteEngine on
RewriteCond ${REQUEST_URI} !\.(?:css|png|js|jpe?g|gif)$ [NC]
RewriteCond ${REQUEST_FILENAME} !-f
RewriteCond ${REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?p=$1 [L,QSA]

This checks if the request is for a file or directory, and if not, it will route it into index.php

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?p=$1 [L,QSA]

You can set your rewrite rules so that they only apply to certain files that don't exist, and others, you can redirect to index.php (or any page desired). Any other file will show up as desired...

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

You can then use a require() to require any type of file that might've been requested, whether it be index.php or someOtherScript.php.

Related