【发布时间】:2019-08-09 21:37:54
【问题描述】:
我有一个有点复杂的重写规则,我无法掌握它。
我想做的是使用 .htaccess 让我的网站看到这个链接:
https://some-domain.com/human/jeff
作为这个链接(因为系统知道如何处理这个链接):
https://some-domain.com/?type=human&which=jeff
注意事项:
- 我只想在
type=human时应用此规则。 -
which参数只能包含字母字符。 - 如果类型是人类,那么除了这些之外,不会有更多的 url 参数。
经过大量尝试,这是我最终失败的 .htaccess 内容:
RewriteEngine On
RewriteRule ^(https://[^/]+)/?\?type=(human)&which=([a-zA-z]+)$ $1/$2/$3 [NC, L]
这是它背后的逻辑:
- 第 1 组:
(https://[^/]+)$1 将包含匹配https://{any non empty string that does not containing "/"}- https://some-domain.com 的任何字符串 - 静态:
/?\?匹配“/?”或“?” - 第 2 组:
type=(human)$2 将包含“人类” - 第 3 组:
which=([a-zA-z]+)$3 将包含一个只有字母字符的非空字符串 (jeff)。
预期结果:
这将匹配正则表达式
https://{any non empty string that does not containing "/"}/human/{any non empty string}
我几乎被困在这里
谢谢!
编辑:
我还应该提到,重写应该只针对 POST 请求,并且需要 post 参数。
【问题讨论】:
-
您在重写模式中使用了无效字符。
http、domain和querystring有不同的东西在RewriteRule中无法匹配。您需要使用RewriteCond指令。不知道下面的答案对您有用。 -
@starkeen 实际上我错了,它没有用。你能帮我遵守规则吗?
标签: regex apache .htaccess url-rewriting