【问题标题】:Apache mod_rewrite with variables带有变量的 Apache mod_rewrite
【发布时间】:2015-05-26 16:50:47
【问题描述】:

我需要修改我的 .htaccess 以执行下一个重定向:

http://www.example.com/en/region1/subregion1/subregion2

http://www.example.com/en/example.php?param1=region1&param2=subregion1&param3=subregion2

对我来说问题是原始 URL 可以有这 3 种不同的结构:

/region1/

/region1/subregion1/

/region1/subregion1/subregion2

我想要的是在变量中转换这个子目录,无论它们是 3 还是只有 1。

如何使用 mod_rewrite 实现这一点?

谢谢

【问题讨论】:

    标签: php apache .htaccess mod-rewrite


    【解决方案1】:

    以此为基础:

    RewriteEngine on
    RewriteRule ^en/(([^/]+)/)(([^/]+)/)?(([^/]+)/)?$ example.php?param1=$2&param2=$4&param3=$6
    

    只有一个参数的 URL 将被定位到

    example.php?param1=region1&param2=&param3=
    

    因此,不存在的参数将为空。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-18
      • 2016-01-22
      • 2018-05-07
      • 1970-01-01
      • 2011-04-07
      • 1970-01-01
      • 2013-03-25
      相关资源
      最近更新 更多