【问题标题】:.htaccess Redirect sub-folder.htaccess 重定向子文件夹
【发布时间】:2011-09-24 18:03:07
【问题描述】:

您好,无论如何我都不是程序员,我正在尝试根据以下内容在我的 htaccess 文件中进行多 301 重定向:

所以我有大量具有相似命名约定的网址 - 这是 2 个示例。

http://www.hollandsbrook.com/garrett-at-gold/
http://www.hollandsbrook.com/garrett-ace-250/

这些网址需要重定向到:

http://www.hollandsbrook.com/garrett-metal-detectors/garrett-at-gold/
http://www.hollandsbrook.com/garrett-metal-detectors/garrett-ace-250/

我可以一次将它们重定向 1 行,但我想使用正则表达式。

这是我到目前为止的想法,但没有奏效:

重写规则 ^garrett-([a-z])/$ /garrett-metal-detectors/$1/ [R]

基本上,我需要将任何以“garrett-”开头的页面重定向到根目录,以包含“garrett-metal-detectors”的文件夹路径。

任何想法将不胜感激。非常感谢您的帮助。

【问题讨论】:

    标签: .htaccess mod-rewrite redirect


    【解决方案1】:

    如果你想要临时重定向使用:

    RewriteRule ^garrett\-([a-z0-9\-]+)/?$ /garrett-metal-detectors/garrett-$1/ [R=302,L]
    

    如果你想永久重定向使用:

    RewriteRule ^garrett\-([a-z0-9\-]+)/?$ /garrett-metal-detectors/garrett-$1/ [R=301,L]
    

    【讨论】:

    • 老兄,你摇滚!我意识到我不想重定向类别页面(garrett-metal-detectors),所以我对您的代码进行了轻微编辑:ewriteRule ^garrett\-([a-ln-z\-]+)/?$ /garrett- metal-detectors/garrett-$1/ [R=301,L] 再次感谢!
    【解决方案2】:

    我不是正则表达式方面的专家,但看起来你的 reg ex 可能有点不对...

    尝试:

    Options +FollowSymLinks
    RewriteEngine on
    RewriteRule ^((garrett)(-[a-z0-9]).*)/$ /metal-detectors/$1/ [R]
    

    这是查找以“garrett”开头的任何内容,后跟任何字母/数字/连字符组合。

    注意:在目标部分包含“garett”会给你一个重定向循环,所以你可能必须选择一个不同的词,或者把它全部删除......

    【讨论】:

      猜你喜欢
      • 2011-02-12
      • 1970-01-01
      • 2021-06-13
      • 2017-09-08
      • 2014-06-01
      • 2011-10-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多