【问题标题】:Mapping URLs with mod rewrite使用 mod rewrite 映射 URL
【发布时间】:2012-09-05 20:55:20
【问题描述】:

我将我的网站迁移到了一个新平台,我想像这样映射旧网址:
anecdotage.com/index.php?aid=13337
对此:
anecdotage.com/articles/13336/
[注意 ID 中 1 的偏移量]

我可以在 apache 的 mod_rewrite 中执行此操作吗?任何帮助表示赞赏!

【问题讨论】:

    标签: regex apache


    【解决方案1】:

    您不能单独使用 mod_rewrite 进行任何数学运算,这意味着您可以匹配 13337 并对其进行操作以将其更改为 13336 而无需使用某种 `RewriteMap 从外部减去一个数字1,但这需要访问服务器或虚拟主机配置。

    因此,您必须在 vhost/server 配置中设置地图:

    RewriteMap subtract prg:/path/to/script/that/subtracts-by-one.sh
    

    这个subtracts-by-one.sh 脚本将一个数字作为输入,从中减去1,然后打印出结果。然后在重写规则中使用它:

    # to make sure we aren't clobbering legit requests
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{QUERY_STRING} aid=([0-9]+)
    RewriteRule ^/?index.php$ /articles/${subtract:%1}/? [L]
    

    如果您无权访问重写地图,那么您将需要通过艰难的方式来完成并列举您所有的重写:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{QUERY_STRING} aid=12345
    RewriteRule ^/?index.php$ /articles/12344/? [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{QUERY_STRING} aid=13337
    RewriteRule ^/?index.php$ /articles/13336/? [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{QUERY_STRING} aid=19911
    RewriteRule ^/?index.php$ /articles/19910/? [L]
    

    等等

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多