【问题标题】:Redirecting a pattern of old URLs in Apache2 and Drupal在 Apache2 和 Drupal 中重定向旧 URL 的模式
【发布时间】:2011-07-08 22:53:47
【问题描述】:

mod_rewrite 一直是我最薄弱的领域之一。一段时间以来,我一直试图找到解决方案,许多答案只是链接到mod_rewrite documentation,这对我没有帮助。我相信我的问题很简单,所以我希望这次有人不会介意给我免费赠品。

我有一些现有的 Drupal 的 mod_rewrite 规则:

重写引擎开启 RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

这当然可以正常工作;它们是现有的 Drupal 规则。

我想重定向请求

 /sn-p/yyyy-MMM-dd/name-of-article

 /sn-ps/name-of-article

所以我把规则改成这样:

重写引擎开启 RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d 重写规则 ^sn-p/20[0-9]{2}-[a-z]{3}-[0-9]{2}/(.*)$ sn-ps/$1 [L,R=301] # ## 我的新规则 RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

我的新规则按预期工作,并正确执行重定向。但是,它似乎会影响 CSS 和 JS 请求,从错误日志中缺少样式和脚本以及 404 错误就可以看出这一点。如果我尝试直接查看 CSS 或 JS 文件,我会得到与请求父文档相同的 HTML 输出。所以,很明显,我的规则被打破了。

我不知道为什么这么难——看起来很简单。

【问题讨论】:

    标签: regex drupal mod-rewrite redirect apache2


    【解决方案1】:

    好吧,我学到了一些东西。 RewriteCond 影响它们之前的 RewriteRule。呵呵!

    这行得通:

    重写引擎开启 RewriteBase / 重写规则 ^sn-p/20[0-9]{2}-[a-z]{3}-[0-9]{2}/(.*)$ sn-ps/$1 [L,R=301] # ## 我的新规则 RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

    【讨论】:

      猜你喜欢
      • 2010-09-28
      • 2012-02-06
      • 2017-08-10
      • 2015-05-02
      • 1970-01-01
      • 2015-09-20
      • 1970-01-01
      • 2014-04-01
      • 1970-01-01
      相关资源
      最近更新 更多