【问题标题】:redirect all directory URLs except one to an external URL using .htaccess使用 .htaccess 将除一个以外的所有目录 URL 重定向到外部 URL
【发布时间】:2015-09-29 18:26:38
【问题描述】:

我需要重定向以下内容:

http://olddomain.com/products/* ---> http://shop.newdomain.com/

http://olddomain.com/products/certain-category除外

到目前为止,我可以将它们全部重定向:

RedirectMatch 301 /products(.*) http://shop.newdomain.com/

我只需要停止重定向“某些类别”??

类似:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond /products(.*) !^/(certain-category)/
RewriteRule (.*) http://shop.newdomain.com/ [L,R=301]
</IfModule>

虽然这会将整个网站重定向到http://shop.newdomain.com/

【问题讨论】:

    标签: regex .htaccess mod-rewrite redirect url-redirection


    【解决方案1】:

    您可以使用这个基于否定前瞻的规则:

    RewriteEngine On
    
    RewriteRule ^products/(?!certain-category).*$ http://shop.newdomain.com/ [NC,L,R=301]
    

    (?!certain-category) 是一个负前瞻,如果certain-category 紧跟在 URL 中的 /products/ 之后,则匹配失败。

    确保在测试之前清除浏览器缓存。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-28
      • 1970-01-01
      • 2012-04-19
      • 2014-07-23
      • 2019-07-13
      • 2016-11-12
      • 2015-06-27
      相关资源
      最近更新 更多