dongra1984 2016-06-18 21:41
浏览 70
已采纳

Apache mod_rewrite:将路径重写为现有文件的查询字符串

I am creating a simple redirect tool for myself in PHP. I'm going to store codes that map to their respective URLs in an associative array, and then redirect anybody who inputs this code to their URL.

I am using query strings to GET the input (the name of the variable and the value, which is the code), and I already know how to make the redirect part work with PHP. The issue is, I don't want my users to have to type in a question mark, the name of the variable, and then an equals sign. I would like for them to simply type in a path that will be converted into a query string, so that PHP can process the redirection.

I was wondering how I could turn a path (an arbitrary URL path) into a query string of an existing file. I was able to find the opposite task quite easily on Stack Overflow (.htaccess - Rewrite query string and redirect to directory) but I want to be able to pass any arbitrary URL path (that does not exist in the directory) into an existing file's query string. I found an example of what I wanted (URL rewrite for converting path to querystring), but it was for multiple query strings and in this case, I only need one. I don't think the last question I referenced was successfully answered either.

How can I redirect

https://www.example.com/r/blahblahblah

into

https://www.example.com/r/index.php?r=blahblahblah

using .htaccess, so that I can parse the query string in my PHP file?

  • 写回答

2条回答 默认 最新

  • dpvm7231 2016-06-18 22:19
    关注

    Something like this:

    RewriteRule ^(.*)$ index.php?r=$1 [NC,L,QSA]
    

    On this URL:

    example.com/r/blahblahblah

    From index.php:

    print_r($_GET);
    

    Will return this:

    Array ( [r] => blahblahblah )
    

    (Edit to mention this assumes .htaccess is in the /r directory)

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

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置