doutu3352 2017-02-28 20:31
浏览 33
已采纳

简单的Apache重写问题传递变量

For some reason I'm having an issue with a rewrite rule that I assume should be very easy.

I would like to rewrite site.com/page.html?1 to site.com/page.php?p=1 and site.com/page.html to site.com/page.php

here is what I assumed should work, but doesn't:

RewriteRule page.html?([^/\.]+)/?$ page.php?p=$1 [L,QSA]
  • 写回答

2条回答 默认 最新

  • dpi74187 2017-02-28 20:43
    关注

    The query string (portion of URL after the ?) is not included in the regex portion of a RewriteRule. Just the requested page path/name. If you want to capture the query string you would need to use a RewriteCond and capture the %{QUERY_STRING} environment variable. Something like this:

    RewriteCond %{QUERY_STRING} (\d*)
    RewriteRule page.html page.php?p=%1 [L,QSA]
    

    Notice, I now capture the query string in the RewriteCond, removed the trailing ?... from the end of the regex in RewriteRule and also changed the variable in the replace with %1 instead of $1 to refer to the captured condition instead of rule. You can also change the regex in the condition to better match what you want.

    Edit: Also, keep in mind that the QSA will append any query string not already present in the rewritten URL. So if you leave that in there, if you are passing a query string like ?1, you will still get a key appended to the URL of 1 with an empty value (?1 => array(1=>'')). If you don't need additional query string values, just remove QSA or if you want the extra values and don't care about blank query string parameters (doesn't hurt unless you are looping over all of $_GET), then leave it in there.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 怎么获取下面的: glove_word2id.json和 glove_numpy.npy 这两个文件
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug