【问题标题】:How to create a RewriteCond and then rewrite this difficult URL using .htaccess如何创建 RewriteCond 然后使用 .htaccess 重写这个困难的 URL
【发布时间】:2013-05-26 09:51:01
【问题描述】:

在阅读并尝试了几个小时的正则表达式之后,我相信是时候寻求帮助了..

我们最近通过将旧文章导入另一个 CMS 和新数据库来迁移一个相当大的网站。 URL 结构完全不同。

旧网址的结构是http://wwww.myurl.com/categoryOLD/article_12345.fixed/this_is_the_title.html

说 /article_12345.fixed/ 的部分是存在于所有旧 URL 中的,就像末尾的 .html 部分一样。 12345 是条目的 ID,每个 URL 都不同。我相信我们可以使用它来识别需要重写的 URL。

旧网址需要改写为http://wwww.myurl.com/categoryNEW/this-is-the-title

所以/article_12345.fix/没了,旧的分类改写成新的分类,最后的.html没了,下划线要变成连字符了。

我一直在阅读和尝试,但我什至无法让 RewriteCond 匹配。有没有人梦想用正则表达式来帮助我?

【问题讨论】:

    标签: .htaccess mod-rewrite rewrite


    【解决方案1】:

    要将下划线重写为连字符,您可以使用 RewriteRule 的 [N] 标志。

    RewriteRule ^([^_]+)_(.*) $1-$2 [N,DPI]
    

    将其放在您的其他规则之前,以便它们与新 URL 一起使用。

    然后重写其余部分的 RewriteRule 可能如下所示:

    RewriteRule ^\/?(.*?)\/(.*?)\/(.*)\.html$ $1/$3
    

    这只是基本的重写,对于基于 ID 的重写 RewriteMap 可能是最好的解决方案,正如 Max Leske 所提到的。

    【讨论】:

    • 谢谢!我现在必须去开会,但会使用你和@max-leske 的提示继续
    • 我在上面的示例中针对 URL 测试了这条规则(其中包含多个 URL 部分),结果导致了无休止的重写循环。我开始记录和阅读,发现这是重写引擎中的一个已知错误。更多关于这个错误的信息在这里:stackoverflow.com/questions/439218/…
    • 抱歉,之前没时间评论,更完整的解释:当使用多部分 URL (/part_1/part_2/etc.) 进行测试时,每次都会将最后一部分添加到新的重写字符串中。所以 /a/b/c 导致 /a/b/c/c。由于 [N] 标志,这个过程在每次“迭代”时都重复,这导致了无限的重写循环。创建 DPI 标志就是为了解决这个问题。它“告诉”引擎它已经附加了该部分。
    【解决方案2】:

    首先:RewriteCondition 不是必需的(请参阅http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecond),所以可以尝试先直接重写,然后在需要时使用条件,以更好地控制匹配流。

    类别

    我会使用rewrite map 重写您的类别,因为您可能需要每个类别一个条目,除非您能想出一个转换规则。

    身份证

    将 id 包含在匹配组中,并且不要在输出中使用该组:
    RewriteRule ^/.*/(<regex for id>/)(.*)$ http://myurl.com$2(仅使用第二个匹配组)

    .html

    与 id 相同:不要使用匹配 .html

    把它放在一起

    所以你的 config / .htaccess 可能看起来像这样:

    RewriteEngine On
    RewriteMap examplemap txt:/path/to/file/map.txt
    RewriteRule ^/.*/([\w\d]+/)(.*)$ ${examplemap:$2}
    
    RewriteRule ^/.*/([article_[\d]+\.fix)(.*)$ http://myurl.com$2
    RewriteRule ^/(.*/)(\w+)_(\w+)(.*)$ http://myurl.com$1$2-$3 [N]
    

    【讨论】:

    • 所以据我了解,map.txt 包含我的旧类别和新类别,每次转换都换行。那么规则会是这样的吗? RewriteMap examplemap txt:/path/to/file/map.txt RewriteRule ^/.*/([a-zA-Z0-9]/)(.*)$ ${examplemap:$2} RewriteRule ^/.*/( article_[0-9].fix/)(.*)$ myurl.com$2 不确定如何处理 RewriteRule ^/(.*)()$ myurl.com$1 因为所有下划线都需要替换用连字符..
    • 看起来差不多。 [查看此答案][stackoverflow.com/questions/1279681/… 了解用破折号替换下划线的可能解决方案。
    • 我添加了可能的(未经测试的)正则表达式模式,最后一个是受我上面发布的链接的启发。
    【解决方案3】:

    我最终使用了以下内容:

    RewriteEngine On
    RewriteRule ^([^_]+)__(.*)\.html$ $1_$2.html [N,DPI]
    RewriteRule ^([^_]+)_(.*)\.html$ $1-$2.html [N,DPI]
    RewriteRule ^(.*)cat1old/(.*)\.html$ $1cat1new/$2.html
    RewriteRule ^(.*)cat2old/(.*)\.html$ $1cat2new/$2.html
    RewriteRule ^(.*)cat3old/(.*)\.html$ $1cat3new/$2.html
    RewriteRule ^(.*)cat4old/(.*)\.html$ $1cat4new/$2.html
    RewriteRule ^\/?(.*)\/(.*)\/(.*)(-?)\.html$ $1/$3 [R=301,L]
    

    像魅力一样工作!谢谢你们的帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-05
      • 1970-01-01
      • 2011-08-18
      • 1970-01-01
      • 2021-04-13
      • 1970-01-01
      • 2012-08-15
      • 1970-01-01
      相关资源
      最近更新 更多