使用htaccess进行自动版本控制:htaccess regex重写规则没有拾取模式

时间:2011-03-20 20:22:05

标签: php regex .htaccess mod-rewrite versioning

使用this question答案中的建议,以及提供几乎相同解决方案的this article,我一直在尝试设置htaccess来处理我的js和css上的自动转换规则文件。

我想要这样做的原因是我更改了它们,但仍希望浏览器长时间缓存它们,而不必在每次更改时手动输入新的版本号。

使用的方法很简单:(1)函数在模式[filename].[version_number].[suffix]上使用更改日期的文件附加版本号,例如,style.css将变为比方说,style.1300638388.css; (2)使用php,版本号包含在我网站页面的样式表声明中,如果版本化文件名与缓存文件名不同,则会向客户端浏览器提供新副本; (3)使用mod_rewrite的.htaccess中的RewriteRule重写版本号,将其恢复为原始值并提供更新后的文件。

下面列出了我在这三个阶段中使用的代码。我正在http://edge.donaldjenkins.net/

上的博客沙箱版本的样式表上对此进行测试

1。在WordPress的functions.php文件

// Allows autoversioning of css and js files

/**
 *  Given a file, i.e. /css/base.css, replaces it with a string containing the
 *  file's mtime, i.e. /css/base.1221534296.css.
 *  
 *  @param $file  The file to be loaded.  Must be an absolute path (i.e.
 *                starting with slash).
 */
function auto_version($file)
{
  if(strpos($file, '/') !== 0 || !file_exists($_SERVER['DOCUMENT_ROOT'] . $file))
    return $file;

  $mtime = filemtime($_SERVER['DOCUMENT_ROOT'] . $file);
  return preg_replace('{\\.([^./]+)$}', ".$mtime.\$1", $file);
}

2。在我的博客文件的<head>部分

<!-- Stylesheets  -->
<link rel="stylesheet" href="<?=auto_version('/path/to/style.css')?>" />

3。在.htaccess文件

# Allow versioning for js and css files

RewriteEngine On
RewriteRule ^(.*)\.[\d]+\.(css|js)$ $1.$2 [L] # Strip out the version number

# End Allow versioning for js and css files

使用上述设置,页面显示时没有任何格式,生成页面的源代码显示样式表被引用为版本号 - 服务器上当然不存在。

这表明该函数正确地更改了样式表的名称,但由于某种原因,.htaccess中RewriteRule中的正则表达式模式没有捕获文件名并重写它。事实上,如果我将其更改为^style.1300638388.css$ style.css [L],它甚至都无法捕获它。

我尝试了几种模式,但没有用,但必须遗漏一些非常基本的模式。

mod_rewrite在服务器上运行,并且在其他几个RewriteRule实例上运行没有问题。

更新

.htaccess文件中唯一的其他RewriteRule是标准的WordPress漂亮网址重写规则。我怀疑它会干扰这个,虽然很明显我不能轻易测试没有WordPress规则,因为这会完全破坏页面生成:

# BEGIN WordPress

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

更新2:已解决

CharlesLeaf在下面的评论中指出,由于WordPress规则是在版本控制之前,后者不会被执行。这实际上是原因,并允许我通过将这两个规则分组来回答问题,如下所示:

<IfModule mod_rewrite.c>

# Allow versioning for js and css files

RewriteEngine On
RewriteRule ^(.*)\.[\d]+\.(css|js)$ $1.$2 [L] # Strip out the version number

# End Allow versioning for js and css files

# BEGIN WordPress

RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php

# END WordPress

</IfModule>

2 个答案:

答案 0 :(得分:3)

^表示'字符串的开头',在这种情况下不是'style'而是/ path / to / style。

您可以使用正则表达式中的完整路径进行尝试,也可以查看RewriteBase。

更新解决方案 这种情况下的问题是在有问题之前还有其他RewriteRules,因此它从未到达此RewriteRule。 RewriteRules的正确顺序很重要。

答案 1 :(得分:2)

感谢您使用.htaccess解决方案。我有jQuery插件文件的问题,在文件名中包含自己的版本控制方案,如jquery.NAME-OF-PLUGIN.1.1.1.js。我建议使用[0-9]{10}解析文件修改时间,以便只删除十位数的后缀。如果文件名末尾有十位数字,那么仍然可能出现错误,但这是一种罕见的边缘情况。

# Allow versioning for js and css files

RewriteRule ^(.*)\.[0-9]{10}\.(css|js)$ $1.$2 [L] # Strip out the version number

# End Allow versioning for js and css files

同样不需要考虑UNIX Epoch时间戳中的11位数字,直到星期六,2286年11月20日17:46:40 GMT。