但后两个层次深断裂的CSS

但后两个层次深断裂的CSS

本文介绍了为什么的.htaccess重定向URL路由正常,但后两个层次深断裂的CSS / JS /图片链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的的.htaccess 在网站的子目录中的文件:

I have the following .htaccess file in a subdirectory of a site:

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]

我有以下的的index.php 文件:

<html>
    <head>
        <link href="css/main.css" rel="stylesheet"/>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
        <script src="js/main.js"></script>
    </head>
    <body>
        <p>if css loads, this should be green</p>
        <p id="message">javascript is NOT loaded</p>
        <div>If images are displaying correctly, then there should be a green square below:</div>
        <img src="images/test.jpg"/>
        <div style="margin: 10px 0 0 0;font-weight: bold;background-color:#eee"><?php echo '$_SERVER["REQUEST_URI"] = '. $_SERVER["REQUEST_URI"]; ?></div>
    </body>
</html>

所有网址是否正确重定向到的index.php。但是,如果该URL为多个级别深度子目录路径的下方,然后我的CSS / JS /图像路径被破坏:

All URLs are properly redirected to index.php. However, if the URL is more than one level deep below the sub-directory path, then my css/js/images paths are broken:

我有什么改变的.htaccess文件,使路由工作在任何级别与不破我的CSS / JS /图像路径?

推荐答案

从绝对URL建议,如果文件始终在域的根,如果你是在html的控制,你可以做领先的斜杠绝对的,这可能会避免对它们进行编辑服务器之间。

From the absolute URL suggestions, if the files are always at the root of the domain, and if you're in control of the html, you could do the leading-slash absolute, which might avoid having to edit them among servers.

/css/main.css
/js/main.js
/images/test.jpg

htaccess的规则可以帮助,如果文件移动,或你不是在html的控制权。例如,我工作的项目中,有些是使用Dreamweaver,它喜欢做../图像/ test.jpg放在

htaccess rules can help if the files move, or you're not in control of the html. e.g., I work on project where some are using dreamweaver, which loves to do ../images/test.jpg.

RewriteRule (css|js|images)/(.+)$ /$1/$2 [NC,QSA,L]

这假定这些文件实际上是在域的根。如果不同服务器之间,那么你就需要编辑htaccess的路径 - 但是这仍然比编辑HTML更容易

This assumes the files are actually at the root of the domain. If that varies among servers, then you'll need to edit the paths in htaccess - but that's still easier than editing the html.

这假定所有的样式,脚本和图像的三个文件夹的CSS,JS,图像;而这些都与这些名字的唯一文件夹。例如,不能有其他一些子文件夹中的另一个图像文件夹。

This assumes all the style, script and images are in the three folders css, js, images; and these are the only folders with these names. e.g., there can't be another images folder in some other subfolder.

这将打破这些字符串,例如,myimages或somecss结尾的文件夹。这可避免通过调整来要求这些特立独行的规则。然后,这些文件夹将不被改变,只有特定的CSS,JS和图片文件夹。

This will break any folder ending in these strings, e.g., myimages or somecss. That can be avoided by tweaking the rule to require these to stand alone. Then those folders won't be changed, only the specific css, js, and images folders.

RewriteRule (?:^|/)(css|js|images)/(.+)$ /$1/$2 [NC,QSA,L]

?(?:^ | /)意味着非捕获,开始 ^ 或斜线后 /

The (?:^|/) means non-capturing ?:, at the beginning ^ or after a slash /.

这篇关于为什么的.htaccess重定向URL路由正常,但后两个层次深断裂的CSS / JS /图片链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 22:42