[英]correct Apache RewriteCond and RewriteRule when using RewriteMap?
[英]Assistance with a RewriteRule using RewriteMap
我对mod_rewrite相当陌生,并且正在尝试使RewriteMap指令正常工作。 感谢您在使用中的帮助。
我们的URL格式如下:
example.com/section.php?xSec=[ID]
因此,对于第201节(即“ Apple iPad”),URL为:
example.com/section.php?xSec=201
我想要的是将其重写为:
example.com/apple-ipads
我已经使用以下行在apache配置中定义了RewriteMap txt文件:
RewriteMap sections txt:/var/www/rewrites/sections.txt
该txt文件包含以下内容:
multifunction-printers 102
inkjet-printers 103
laser-printers 104
apple-ipads 201
我尝试在.htaccess中使用以下重写规则:
RewriteRule ^/section.php?xSec=(.*) ${sections:$1}
这不起作用。 我假设RewriteRule存在问题,或者我将RewriteMap指令放在配置文件中的错误位置。 有什么建议么?
提前致谢
丹尼尔
您可以使用以下重写:
RewriteRule ^/(.*) /section.php?xSec=${sections:$1|NOTFOUND}
对于发送到的每个请求:
example.com/apple-ipads
您应该透明地看到答案来自:
example.com/section.php?xSec=201
如果找不到密钥,则答案来自:
example.com/section.php?xSec=NOTFOUND
但是您必须知道,这种重写是行不通的,例如,当您有空格或其他字符(àèò...)可以由浏览器编码时。
您的地图向后。 该密钥应首先列出。
201 apple-ipads
我相信您不应该在模式的开头加上/以及其他一些东西; 也就是说,假设您的ID仅是数字,则更好的模式是:
RewriteRule ^section\.php\?xSec=(\d+)$ ${sections:$1} [nocase]
我只需要用uri做一些htaccess的工作,而我需要grep查询字符串。 这篇出色的文章对我们很有帮助。 以前的模式不起作用。 这个应该:
RewriteCond %{QUERY_STRING} xSec=(\d+)$ [nocase]
RewriteRule ^section\.php$ ${sections:%1}? [nocase,redirect=perm,last]
如果要在键不匹配(未调试)时默认使用原始查询字符串:
RewriteCond %{QUERY_STRING} xSec=(\d+)$ [nocase]
RewriteRule ^section\.php$ ${sections:%1?|$0?id=%1} [nocase,redirect=perm,last]
最后一个示例中的语法是错误的,因为? 在$ {}函数的匹配端。 此后,我使用以下内容更新了自己的规则:
# mapped
RewriteCond %{QUERY_STRING} xSec=(\d+)$ [nocase]
RewriteCond ${sections:%1|NOTFOUND} !NOTFOUND [nocase]
RewriteRule ^section\.php$ ${sections:%1}? [nocase,redirect=perm,last]
使用这组规则,只有带有映射(查询)键的uri才会被重定向。
干杯。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.