Access relative CSS/JS files with htaccess rewrite rule

后端 未结 2 400
遥遥无期
遥遥无期 2021-01-22 07:10

I was recently asked if I could make my friends server address more user friendly. His current urls looks like this:

http://wwww.example.com/site/index.php?page=         


        
相关标签:
2条回答
  • 2021-01-22 07:19

    Better to use this rule:

    RewriteEngine On
    RewriteBase /site/
    
    RewriteCond %{REQUEST_FILENAME} !/(admin|css|fonts|ico|include|js)/
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.+?)/?$ index.php?page=$1 [L,QSA]
    

    Then for css/js/images better to use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.

    Alternatively You can try adding this in your page's header:

    <base href="/" />
    

    OR

    <base href="http://domain.com/site/" />
    
    0 讨论(0)
  • 2021-01-22 07:32

    Is there something I could change or add in my htaccess file to get the css and js files to load with a relative path? If so what would I need to do?

    You could just add the proper relative URI base in your page header:

    <base href="/site/" />
    

    Or you could brute force redirect them using mod_rewrite (not preferable):

    RewriteRule ^(.+)/(admin|css|fonts|ico|include|js)/(.*)$ $2/$3 [L]
    
    0 讨论(0)
提交回复
热议问题