重写的CSS / JS路径 [英] Rewrite css/js paths
问题描述
所以我改写了我的路径是这样的: URL /真的/漂亮/路径/
使用这样的mod_rewrite规则:
So I rewrote my paths to something like: URL/really/nice/paths/
using mod_rewrite rules like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
现在的问题是我怎么能重写路径JS / CSS /图像文件了,所以当他们被要求的使用相对路径从 URL /真的/漂亮/路径/
从 URL /脚本/
, URL /风格/
并担任 URL /图片/
文件夹呢?可以这样不用做的RewriteBase
?
The question is how could I rewrite the paths for js/css/image files too, so when they are requested with a relative path from URL/really/nice/path/
to be served from URL/scripts/
, URL/styles/
and URL/images/
folders instead? Can this be done without using RewriteBase
?
推荐答案
当URL被改写,客户端不知道这一点。因此,当一个客户机着眼于一个页面的URLexample.com/some/url/和页面引用的图像中的图像/ image.jpg文件时,客户端查找图像中的example.com/some/ URL /图像/ image.jpg的即使页面实际驻留在example.com/some/other/url/。这就是你面临的问题,对吧?
When URLs are rewritten, the client doesn't know it. So when a client looks at a page at the URL "example.com/some/url/" and the page references an image in "images/image.jpg", the client looks for the image in "example.com/some/url/images/image.jpg" even though the page actually resides in "example.com/some/other/url/". That's the problem you're facing, right?
有三个主要的解决方案,这一问题:
There are three main solutions to this problem:
- 使用绝对路径的资源,而不是相对的。
- 使用
&LT;基地&GT;
标签确保客户知道赖以建立其相对URL根是从页面的URL明显不同。 - 在添加新的规则在您的重写规则部分/ URL /照片/。
选项1,可能是最好的主意,你会发现,使用URL重写大多数网站使用,包括堆栈溢出本身。选项2令人难以接受的,但工作是比较容易的。方案3是最难以维持,因为URL重写例外和特殊情况下可以显示为您定义新的规则。
Option 1 is probably the best idea, and you'll find that most sites that use URL rewriting use it, including Stack Overflow itself. Option 2 is frowned upon, but works and is relatively easy. Option 3 is the most difficult to maintain, as URL rewriting exceptions and special cases can appear as you're defining new rules.
最易于维护的解决方案是使用绝对URL。
The most maintainable solution is to use absolute URLs.
这篇关于重写的CSS / JS路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!