skip to Main Content

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


  1. 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]
    
    Login or Signup to reply.
  2. 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]
    
    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search