mod_rewrite相对路径css/js的问题

发布于 2024-11-04 05:25:54 字数 560 浏览 2 评论 0原文

嗨,我有一个问题。 我想获取重定向到主目录中索引文件的所有请求,并且我已经实现了这一点,但相对路径存在问题。
当我输入类似以下地址:mydomain.com/something 时,它可以正常工作,因为路径是相对于主目录的。
问题是当我输入类似以下内容时:mydomain.com/something/somethingelse。

.htaccess 文件:


Options FollowSymLinks
RewriteEngine On
# ignore anything that's an actual file
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
# redirect all other traffic to the index page
RewriteRule . index.php [L]

关于如何让 css/js 工作有什么想法吗?


编辑:

问题是当输入的路径有多个斜杠时,不会加载 css/js 文件,例如:mydomain.com/something/somethingelse

Hi I have a problem.
I want to get all requests to redirect to index file in main directory and I've achieved this but there are problems with relative paths.

When I put address like: mydomain.com/something it works ok as the paths are relative to the main directory.

The problem is when I put something like: mydomain.com/something/somethingelse.

the .htaccess file:


Options FollowSymLinks
RewriteEngine On
# ignore anything that's an actual file
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
# redirect all other traffic to the index page
RewriteRule . index.php [L]

Any ideas on how to get css/js working?


Edit:

The problem is that css/js files aren't loaded when the path entered have multiple slashes
like:mydomain.com/something/somethingelse

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

盛夏已如深秋| 2024-11-11 05:25:54

毫无疑问,对于静态文件(css、js、图像等)使用绝对路径更好。但是,如果您在多个页面中存在大量此类实例,请考虑使用 HTML 基本标记 来指定相对路径的默认 URL。例如:

<base href="http://www.example.com/static/" />

It is no doubt better to use absolute path for static files (css, js, images etc). But if you lots of those instances in several pages then consider using HTML base tag to specify a default URL for relative paths. eg:

<base href="http://www.example.com/static/" />
回心转意 2024-11-11 05:25:54

使用 标签是一个很好的解决方案,大多数浏览器似乎都能很好地处理它。除了 IE 存在一些问题之外,正如预期的那样......显然您还可能遇到其他一些有趣的问题,请参阅此处的讨论

因此,对于那些无法选择这种方法的人,我已经研究了替代方案(“困难的方法”)。

通常,您存储 css/js/静态图像/其他类似这样的东西:

index.php
js/
css/
imgs/

并且您希望 javascript 和样式表等可用,无论 url 中有多少个斜杠。如果你的url是/site/action/user/new那么你的浏览器会请求

/site/action/user/css/style.css
/site/action/user/css/framework/fonts/icons.ttf
/site/action/user/js/page.js
/site/action/user/js/jquery/jquery.min.js
/site/action/user/js/some/library/with/deep/dir/structure/file.map

所以这里有一些apache的重写规则来解决这个问题......首先,如果目标实际上存在于磁盘上,则不要重写:

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [L,QSA]

换句话说,如果请求文件名是一个目录,或者如果请求文件名是一个文件,则不重写 (-),最后一条规则 (L) 并传递任何 GET 参数(QSA、查询字符串附加)。 也可以使用

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^.*$ - [L,QSA]

如果您还需要符号链接, 。接下来,即使请求假设了错误的基目录(如上所示),我们也希望能够找到 javascript 和样式表。

RewriteRule ^.*/js/(.*)$ js/$1 [L]      
RewriteRule ^.*/css/(.*)$ css/$1 [L]

模式非常明显,只需将“css”替换为目录名称即可。这仍然存在一个问题,特别是对于具有大量 javascript 和样式表、库等的大型网站。 - 正则表达式是贪婪的。例如,如果您有一个像这样的 javascript 目录:

js/some/library/js/script.js

并且您的请求转到 /site/action/user/new,浏览器将请求 /site/action/user/new/js /some/library/js/script.js,重写引擎将重写它,

js/script.js

因为第一个 .* 是贪婪的并且匹配 /site/action/user/新/js/some/library。切换到非贪婪正则表达式并没有真正意义,因为“重写引擎会重复所有规则,直到 URI 成为通过规则进行迭代之前和之后

是相同的。” 还有一个问题,那就是对于每个需要免于重写的目录,都需要一个相对“昂贵”的正则表达式。这两个问题都可以通过将每个静态组件放入一个具有“不寻常”名称的子目录中来解决(在我看来,这实际上是最好的解决方案 - 任何有更好想法的人请发布它)。

目录结构将如下所示:

index.php
mystrangedir/js/
mystrangedir/css/
mystrangedir/imgs/

当然,这需要插入到代码中的任何位置 - 对于具有大量现有代码库的项目,这可能很棘手。但是,您只需要一个正则表达式来进行目录豁免:

RewriteRule ^.*/mystrangedir/(.*)$ mystrangedir/$1 [L]

自动构建系统(如 gulp、grunt...)可用于检查“mystrangedir”是否不作为其自身下方任何位置的目录存在(这将再次抛出重写引擎)。

请随意将 mystrangedir 重命名为更明智的名称,例如 static_content,但它越明智,该目录名称就越有可能已在某些库中使用。如果您想要一个绝对安全且以前从未使用过的目录名称,请使用加密哈希,例如010f8cea4cd34f820a9a01cb3446cb93637a54d840a4f3c106d1d41b030c7bcb。这是相当长的匹配;您可以通过缩短它来在唯一性和正则表达式性能之间进行权衡。

Using the <base>-tag is a nice solution and most browsers seem to handle it well. Except there are some issues with IE, as was to be expected... Apparently you can also run into some other funny problems, see discussion here.

So for people where this is not an option, i have looked into the alternative (the "hard way").

Usually you store css/js/static images/other stuff like this:

index.php
js/
css/
imgs/

and you want the javascript and stylesheets etc. to be available, no matter how many slashes there are in the url. If your url is /site/action/user/new then your browser will request

/site/action/user/css/style.css
/site/action/user/css/framework/fonts/icons.ttf
/site/action/user/js/page.js
/site/action/user/js/jquery/jquery.min.js
/site/action/user/js/some/library/with/deep/dir/structure/file.map

So here are some rewrite rules for apache to solve this... First, if the target actually exists on disk, do not rewrite:

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [L,QSA]

In words, IF reqest filename is a directory OR IF request filename is a file then do not rewrite (-), last rule (L) and pass any GET parameters (QSA, query string append). You can also use

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^.*$ - [L,QSA]

if you also need symlinks. Next we want the javascript and stylesheets to be found even if the requests assume a wrong base directory as shown above.

RewriteRule ^.*/js/(.*)$ js/$1 [L]      
RewriteRule ^.*/css/(.*)$ css/$1 [L]

The pattern is pretty obvious, just replace 'css' with the directory name. There is still a problem with this, especially for large websites with lots of javascript and stylesheets, libraries etc. - The regex is greedy. For example, if you have a javascript directory like this:

js/some/library/js/script.js

and your request goes to /site/action/user/new, the browser will request /site/action/user/new/js/some/library/js/script.js, which the rewrite-engine will then rewrite to

js/script.js

because the first .* is greedy and matches /site/action/user/new/js/some/library. Switching to non-greedy regex does not really make sense, since "the rewrite engine repeats all the rules until the URI is the same before and after an iteration through the rules."

There is another problem, and that is that for every directory that needs to be exempted from rewriting, a relatively "expensive" regex is needed. Both problems can be fixed by just putting every static component into a subdirectory with an "unusual" name (and really this is the best solution imo - anyone with a better idea please post it).

The directory structure would then look like this:

index.php
mystrangedir/js/
mystrangedir/css/
mystrangedir/imgs/

Of course, this needs to be inserted everywhere in the code - for projects with a large existing codebase this can be tricky. However, you only need a single regex for directory exemption then:

RewriteRule ^.*/mystrangedir/(.*)$ mystrangedir/$1 [L]

Automated build systems (like gulp, grunt....) can be used to check if "mystrangedir" does not exist as directory anywhere below itself (which would again throw off the rewrite engine).

Feel free to rename mystrangedir to something more sensible like static_content but the more sensible it gets, the more probable it is that the directory name is already used in some library. If you want an absolutely safe directory name that has certainly never been used before, use a cryptographic hash, e.g. 010f8cea4cd34f820a9a01cb3446cb93637a54d840a4f3c106d1d41b030c7bcb. This is pretty long to match; you can make a tradeoff between uniqueness and regex performance by shorting it.

梦太阳 2024-11-11 05:25:54
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]

尽管有评论,显然应该有效。

尝试添加 RewriteLog 和 RewriteLogLevel 指令来为我们提供更好的详细信息。

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]

Should obviously work despite the comments.

Try to add the RewriteLog and RewriteLogLevel directive to give us better details.

白云悠悠 2024-11-11 05:25:54

这是一个路径解析问题:当在基本路径 /something 上使用相对路径 ./css 时,它会解析为 /css /something/somethingelse 它被解析为 /something/css

这不能(或者更确切地说不应该)用 mod_rewrite 来修复。使用绝对路径而不是相对路径,因此使用 /css 而不是 ./css

This is a path resolution issue: When using the relative path ./css on the base path /something it is resolved to /css while on /something/somethingelse it is resolved to /something/css.

This can’t (or rather shouldn’t) be fixed with mod_rewrite. Use absolute paths instead of relative paths, so /css instead of ./css.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文