Emmanuel Joshua
Emmanuel Joshua

Reputation: 94

htaccess single page entry not serving static files

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]

Upvotes: 2

Views: 288

Answers (3)

user2182349
user2182349

Reputation: 9782

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]

Upvotes: 1

HoldOffHunger
HoldOffHunger

Reputation: 20901

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.

Upvotes: 0

Croises
Croises

Reputation: 18671

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]

Upvotes: 2

Related Questions