【问题标题】:.htacces css/js subdirectory issue.htaccess css/js 子目录问题
【发布时间】:2011-12-24 17:14:26
【问题描述】:

我正在尝试对站点的 .htaccess 文件进行一些更改。我在网上搜索过,但我找不到明确的解决方案,或者有可能我弄错了。所以我们在这里:


RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule   ^/$  /e/www/  [R]
RewriteRule ^news$ /news.php
RewriteRule ^news$ /news.php
RewriteRule ^resources$ /resources.php
RewriteRule ^lexicon$ /lexicon.php
RewriteRule ^contacts$ /contacts.php
RewriteRule ^analytics$ /analytics.php
RewriteRule ^([0-9]+)-([0-9]+)-([0-9]+)-(.*)$ /news.php?yy=$1&mm=$2&dd=$3&alias=$4
RewriteRule ^resources$ /contacts.php
RewriteRule ^articles_search$ /articles_search.php
RewriteRule ^articles_data_search$ /articles_data_search.php
RewriteRule ^profile/([0-9]+)&commit=(.*)$ /profile.php?id=$1&commit=$2
RewriteRule ^profile/(.*)&commit=(.*)$ /profile.php?alias=$1&commit=$2

在最后 2 行,我试图从中得到结果:

www.example.com/profile.php?alias=kokazani&commit=Edit

到这里:

www.example.com/profile/kokazani&commit=Edit

现在的问题是css和js文件路径错误。我需要一个解决方案,或者更好的重写规则的方法。我认为一个解决方案可能是将css/blabla.css之类的所有内容重写到根目录/css/blabla.css,但是如果有解决这个问题的标准解决方案,我想知道:)

提前致谢。

【问题讨论】:

  • 代码格式化是在每行代码之前使用 4 个空格缩进完成的。请参阅faqediting help
  • 问个简单的问题 - 你确定要/profile/kokazani&commit=Edit 而不是/profile/kokazani?commit=Edit
  • 有什么好处?而不是 & 用法?

标签: php html .htaccess mod-rewrite url-rewriting


【解决方案1】:

我的方法是(如 Truth 所建议的)使用绝对路径。如果这不可能,您可以使用:

RewriteRule ^(.+)/css/(.*)$ /css/$2

但请注意:这意味着客户端(浏览器)最终会获取并缓存同一个文件的多个副本,因为它是从不同的路径获取它(就它所能看到的而言)。因此,您将失去客户端样式表缓存的优势。

【讨论】:

    【解决方案2】:

    有两种可能的(简单)解决方案:

    • 为您的 CSS 文件使用绝对路径(/include/css/style.css 等)
    • 使用 HTML 的 <base> 元素为要遵循的相关 URL(而不是被 Apache 更改的文档目录)设置正确的基本 URL。

    【讨论】:

    • 我只使用 解决方案。一切都干净而清晰。如果我对此有任何其他问题,我会戳你:) 再次感谢。
    猜你喜欢
    • 2021-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多