CSS, JS and images do not display with pretty url

后端 未结 4 1599
猫巷女王i
猫巷女王i 2020-11-27 23:18

I am trying to rewrite the URL through the htaccess file so that the following URL

www.domain.com/subfolder/index.php?key

can be accessed b

相关标签:
4条回答
  • 2020-11-28 00:04

    Did you try the base URL? That is the best solution I have come across. Just put one in the head and then you can even give it an ID and access the URL with javascript.

    <base id='baseElement' href="your path goes here">  <!--possibly generated with serverside code-->
    

    Access with jquery

    var base = $('#baseElement').attr('href');
    
    0 讨论(0)
  • 2020-11-28 00:07

    Actually there is a solution with .htaccess !

    Assuming that your index.php is located in /mysite/ and your ressources files are in /mysite/scripts/, /mysite/images/ and/mysite/style/ like mines, you could go like this :

    RewriteEngine on
    #first exclude those folders
    RewriteBase "/mysite/"
    RewriteRule ^(style|scripts|images)($|/) - [L]
    #then go with your rules (the [L] terminates the ruling)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteCond %{DOCUMENT_ROOT}/$1 -f
    RewriteRule ^[^/]+/([^.]+\.(?:js|css|jpe?g|png|gif))$ /$1 [L,R=301,NC]
    RewriteRule ^index.php/([a-zA-Z0-9/_]+)$ index.php?key=$1
    

    You can see .htaccess mod_rewrite - how to exclude directory from rewrite rule for few other ways

    0 讨论(0)
  • 2020-11-28 00:12

    Also, you can try to add this to your HTaccess:

    RewriteBase /
    
    0 讨论(0)
  • 2020-11-28 00:18

    When you use relative url's, the browser will dynamically create a complete url by using the url of the resource it loaded. In other words: It uses the url as it is displayed in the address bar. In your case (www.domain.com/subfolder/index.php/key) it tries to load any relative url relative to www.domain.com/subfolder/index.php/. Your resources are however not located there.

    You have two options to resolve this problem:

    • Convert your relative url's into absolute url's, at least absolute to the domain root. Something like <img src="img/unicorn.png"> should be turned into <img src="/path/to/img/unicorn.png">.

    • Add a base to your head element. This base will be used instead of the url of the resource to calculate the complete url. You should add <base href="/"> to your <head> element. / will then be used as the base of any relative url.

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