我是mod_rewrite的新手,我正在尝试让RewriteMap指令正常工作。如果您能帮助我使用它,我将不胜感激。
我们的URL格式如下:
example.com/section.php?xSec=[ID]因此,对于第201节,即'Apple iPads‘,网址是:
example.com/section.php?xSec=201我想要的是将其重写为:
example.com/apple-ipads我已经在apache config中使用以下行定义了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指令放在了配置文件中的错误位置。有什么建议吗?
提前感谢
丹尼尔
发布于 2012-11-29 18:43:17
您可以使用此重写:
RewriteRule ^/(.*) /section.php?xSec=${sections:$1|NOTFOUND}对于发送到的每个请求:
example.com/apple-ipads你应该清楚地看到答案来自于:
example.com/section.php?xSec=201如果找不到密钥,答案来自:
example.com/section.php?xSec=NOTFOUND但您必须知道,这种重写不能工作,例如,当您有一个空格或其他字符时(àèoó...)可以由浏览器进行编码的。
发布于 2013-03-26 00:24:06
你的地图是反向的。密钥应该列在第一位。
201 apple-ipads我认为你不应该在模式的开头加上其他一些东西;也就是说,假设你的I只是数字,一个更好的模式应该是:
RewriteRule ^section\.php\?xSec=(\d+)$ ${sections:$1} [nocase]更新
我只需要用uri做一些htaccess工作,在那里我需要grep查询字符串。这个excellent post有很大的帮助。之前的模式将不起作用。这个应该是:
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]更新2
最后一个示例中的语法是错误的,因为?在${}函数的匹配端。自那以后,我使用如下内容更新了我自己的规则:
# mapped
RewriteCond %{QUERY_STRING} xSec=(\d+)$ [nocase]
RewriteCond ${sections:%1|NOTFOUND} !NOTFOUND [nocase]
RewriteRule ^section\.php$ ${sections:%1}? [nocase,redirect=perm,last]使用这组规则,只有具有映射(查询)键的uri才会被重定向。
干杯。
https://stackoverflow.com/questions/13623195
复制相似问题