How to force .htaccess used for routing to not route .css, .js, .jpg, etc. files?

前端 未结 4 1789
無奈伤痛
無奈伤痛 2020-12-09 11:29

I have the following .htaccess file in a subdirectory of a site which allows me to route all URLs to index.php where I can parse them.

However, it\'s not al

4条回答
  •  Happy的楠姐
    2020-12-09 12:24

    Something I noticed. You're using the forward slash instead of a question mark... the params redirect would normally look like this:

    RewriteRule ^(.*)$ index.php?params=$1 [L,QSA]
    

    This should work by itself since any of those files *should* be real files.

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

    To have the site ignore specific extensions you can add a condition to ignore case and only check the end of the filenames in the request:

    RewriteEngine On
    
    RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?params=$1 [L,QSA]
    

    If you're trying to ignore a folder then you could add:

    RewriteEngine On
    
    RewriteCond %{REQUEST_URI} !(public|css)
    RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?params=$1 [L,QSA]
    

提交回复
热议问题