How do I exclude CSS, JS, JPG, GIF files from mod_rewrite rules?

前端 未结 4 1087
挽巷
挽巷 2021-01-14 06:26

This is what I have so far:

RewriteRule ^([A-Za-z0-9_]*)/?$ index.php?page=$1 [NC]
RewriteRule ^([A-Za-z0-9_]*)/([A-Za-z0-9_]*)/?$ index.php?page=$1/$2 [NC]
         


        
4条回答
  •  孤街浪徒
    2021-01-14 07:05

    I guess my solution will be the most empirical one but it worked:

    RewriteEngine On
    
    RewriteBase /
    
    RewriteRule ^([a-z0-9-]+)$ /?page=$1 [NC,L]
    
    RewriteRule ^([a-z0-9-]+)/([a-z0-9-]+)$ /?page=$1&action=$2 [NC,L]
    

    That will translate http://example.com/?page=contact&action=send to http://example.com/contact/send My page values were only alphanumeric and contain minus sign as word separator but you can replace the values to match whatever you need.

    And, of course you need to prefix all of your js, css, img directories with a slash in the pages. So css/style.css would be /css/style.css or use absolute url.

提交回复
热议问题