Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 将查询字符串参数重写为URL Apache_Regex_Apache_.htaccess_Query String - Fatal编程技术网

Regex 将查询字符串参数重写为URL Apache

Regex 将查询字符串参数重写为URL Apache,regex,apache,.htaccess,query-string,Regex,Apache,.htaccess,Query String,进入Apache的URL: localhost:8888/game?level=0 应来自apache的URL: localhost:8888/game?level=0 localhost:8888/level0/game 有人能帮我重写一下吗 localhost:8888/game?level=0 试图通过以下方式解决此问题: localhost:8888/game?level=0 RewriteEngine On RewriteRule ^game\/+(.*?)\?+level=

进入Apache的URL:

localhost:8888/game?level=0
应来自apache的URL:

localhost:8888/game?level=0
localhost:8888/level0/game
有人能帮我重写一下吗

localhost:8888/game?level=0
试图通过以下方式解决此问题:

localhost:8888/game?level=0
RewriteEngine On RewriteRule ^game\/+(.*?)\?+level=+([0-9]+) /level$2/$1 [L,QSA] 重新启动发动机 重写规则^game\/+(.*?)+等级=+([0-9]+)/等级$2/$1[L,QSA] 没有运气,因为它不匹配

localhost:8888/game?level=0
谢谢,
Peter

为了能够从查询字符串中捕获值,您需要一个
RewriteCond%{query_string}
指令,以便捕获的组可以作为
%n
占位符在以下重写规则中使用:

localhost:8888/game?level=0
RewriteCond %{QUERY_STRING} ^level=(.*)$
RewriteRule ^game /level%1/game [L]

如果在浏览器中键入
/game?level=0
,apache将打开
/level0/game
;无需将查询字符串附加到重写的url(
QSA
标志)

您可以在
文档的根目录中使用此代码

localhost:8888/game?level=0
RewriteEngine On
RewriteBase /

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /(game)\?(level)=(\d+) [NC]
RewriteRule ^ %2%3/%1? [R=302,L,NE]

# internal forward from pretty URL to actual one
RewriteRule ^(level)(\d+)/([^/.]+)/?$ $3?$1=$2 [L,QSA,NC]

如果您提供了您试图解决此问题的方法,您会得到更多帮助。@hwnd添加了我尝试过的解决方案。您确定要采用这种重写方向(使某个对象看起来是静态的?)重写规则通常是以相反的方式编写的。无论如何,在RewriteRule中,您将能够只匹配url路径(没有查询字符串)。您需要
RewriteCond%{QUERY\u STRING}…..
来捕获查询字符串值,请参见@guido yesp,这是正确的方向。所以,添加RewriteCond%{QUERY_STRING}游戏仍然无法修复它。。请您选择第五个方向好吗?这似乎不起作用,当我复制粘贴这些设置并打开开始url时,我发现找不到请求的url/游戏在此服务器上找不到。配合^game@Peter起始url是什么?文档根目录下是否存在该上下文的
/level0/game
文件?@Peter啊抱歉,我错过了您第一条评论中关于^game的最后一篇文章,我有一个typo@anubhava是的,他得到了不寻常的要求,正如你所看到的,我确保在问题下面的评论中提问