.htaccess redirecting when file doesn't exists

前端 未结 1 1557
北海茫月
北海茫月 2021-02-10 06:33

I\'m using .htaccess to redirect users to my main controller and it is working fine. But when i call a js file that doesn\'t exist like:



        
1条回答
  •  生来不讨喜
    2021-02-10 06:51

    Well, the way that you have it setup, everything gets routed through index.php. If a request is made for a resource that doesn't exist, it gets routed through index.php. It's up to index.php to realize something's not there, and return a 404 error, but I'm guessing it's setup so that if the url= doesn't exist, to returns the home page.

    You can change your rules to ignore the routing for certain files, like for your example:

    RewriteEngine on
    RewriteCond %{REQUEST_URI} !^/js/
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
    

    This would make it so nothing in the /js directory will ever get routed through index.php, so if someone requests a non-existing file in the /js directory, they'll just get the regular 404 response.


    EDIT:

    That's what I want, to ignore the routing for certain files (js, css, images), because then if the files doesn't exists i would get the normal error!

    If you want to ignore all images, javascript, and css, then the 2nd line should be:

    RewriteCond %{REQUEST_URI} !\.(png|jpe?g|gif|css|js)$ [NC]
    

    0 讨论(0)
提交回复
热议问题