【问题标题】:Can these .htaccess mod_rewrite rules be compacted?这些 .htaccess mod_rewrite 规则可以压缩吗?
【发布时间】:2012-04-27 11:04:39
【问题描述】:

我的 htaccess 文件中有一些 mod_rewrite 规则可以将不存在的页面重定向到默认页面,如下所示:

  • http://domain.tld/category1/subcategoryX
  • http://domain.tld/category1/subcategoryX/
  • http://domain.tld/category1/subcategoryX/property1
  • http://domain.tld/category1/subcategoryX/property1/

全部重定向到

  • http://domain.tld/category1/subcategoryX/property1/value1

我正在执行此重定向,因为子类别和属性没有页面,只有值有。

这些是规则:

RewriteRule ^category1/[a-z-]+$             http://%{HTTP_HOST}%{REQUEST_URI}/property1/value1 [R,NC,L]
RewriteRule ^category1/[a-z-]+/$            http://%{HTTP_HOST}%{REQUEST_URI}property1/value1 [R,NC,L]
RewriteRule ^category1/[a-z-]+/property1$   http://%{HTTP_HOST}%{REQUEST_URI}/value1 [R,NC,L]
RewriteRule ^category1/[a-z-]+/property1/$  http://%{HTTP_HOST}%{REQUEST_URI}value1 [R,NC,L]

规则是有效的,但它们有点冗长。有什么技巧可以压缩规则吗?前两个的区别只是结束斜线,后两个也是。

【问题讨论】:

    标签: .htaccess mod-rewrite url-rewriting rewrite redirect


    【解决方案1】:
    RewriteRule ^(category1/[a-z-]+)/?$             /$1/property1/value1 [R,NC,L]
    RewriteRule ^(category1/[a-z-]+/property1)/?$   /$1/value1 [R,NC,L]
    

    或者您甚至可以重定向所有以 category1/*

    开头的 url
    RewriteRule ^(category1/[a-z-]+)  /$1/property1/value1 [R,NC,L]
    

    【讨论】:

      猜你喜欢
      • 2011-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-27
      • 2016-08-31
      相关资源
      最近更新 更多