简体   繁体   English

使用RewriteMap协助RewriteRule

[英]Assistance with a RewriteRule using RewriteMap

I'm fairly new to mod_rewrite and I'm trying to get the RewriteMap directive to work properly. 我对mod_rewrite相当陌生,并且正在尝试使RewriteMap指令正常工作。 I would appreciate your help in using it. 感谢您在使用中的帮助。

Our URL format is as follows: 我们的URL格式如下:

example.com/section.php?xSec=[ID]

So for section 201, which is 'Apple iPads' the URL is: 因此,对于第201节(即“ Apple iPad”),URL为:

example.com/section.php?xSec=201

What I'm wanting is to rewrite this to: 我想要的是将其重写为:

example.com/apple-ipads

I have defined the RewriteMap txt file in apache config, using the following line: 我已经使用以下行在apache配置中定义了RewriteMap txt文件:

RewriteMap sections txt:/var/www/rewrites/sections.txt

This txt file contains the following: 该txt文件包含以下内容:

multifunction-printers 102
inkjet-printers 103
laser-printers 104
apple-ipads 201

I have attempted the following rewrite rule in .htaccess: 我尝试在.htaccess中使用以下重写规则:

RewriteRule ^/section.php?xSec=(.*) ${sections:$1}

Which doesn't work. 这不起作用。 I'm assuming that there is either an issue with my RewriteRule or I've put the RewriteMap directive in the wrong place in the config file. 我假设RewriteRule存在问题,或者我将RewriteMap指令放在配置文件中的错误位置。 Any suggestions? 有什么建议么?

Thanks in Advance 提前致谢

Daniel 丹尼尔

You could use this rewrite: 您可以使用以下重写:

RewriteRule ^/(.*) /section.php?xSec=${sections:$1|NOTFOUND}

For every request sent to: 对于发送到的每个请求:

example.com/apple-ipads

You should see transparently the answer come from: 您应该透明地看到答案来自:

example.com/section.php?xSec=201

In case the key is not found, the answer come from: 如果找不到密钥,则答案来自:

example.com/section.php?xSec=NOTFOUND

But you must know that this rewrite could not work, for example when you have a space or other characters (àèò...) that can be encoded by the browser. 但是您必须知道,这种重写是行不通的,例如,当您有空格或其他字符(àèò...)可以由浏览器编码时。

your map is backwards. 您的地图向后。 the key should be listed first. 该密钥应首先列出。

201 apple-ipads

I believe you shouldn't include the / at the beginning of the pattern, plus a few other things; 我相信您不应该在模式的开头加上/以及其他一些东西; ie, assuming your IDs are numbers only, a better pattern would be: 也就是说,假设您的ID仅是数字,则更好的模式是:

RewriteRule  ^section\.php\?xSec=(\d+)$  ${sections:$1}  [nocase]


Update 更新

I've just had to do some htaccess work with uri's where I needed to grep the query strings. 我只需要用uri做一些htaccess的工作,而我需要grep查询字符串。 this excellent post helped greatly. 这篇出色的文章对我们很有帮助。 the previous pattern won't work. 以前的模式不起作用。 this one should: 这个应该:

RewriteCond   %{QUERY_STRING}   xSec=(\d+)$   [nocase]
RewriteRule   ^section\.php$   ${sections:%1}?  [nocase,redirect=perm,last]

If you want to default to using the original query string when the key can't be matched (untested): 如果要在键不匹配(未调试)时默认使用原始查询字符串:

RewriteCond   %{QUERY_STRING}   xSec=(\d+)$   [nocase]
RewriteRule   ^section\.php$   ${sections:%1?|$0?id=%1}  [nocase,redirect=perm,last]


Update 2 更新2

The syntax in the last example is wrong because of the ? 最后一个示例中的语法是错误的,因为? inside the matching side of the ${} function. 在$ {}函数的匹配端。 I've since updated my own rules using something like this: 此后,我使用以下内容更新了自己的规则:

# mapped
RewriteCond   %{QUERY_STRING}   xSec=(\d+)$   [nocase]
RewriteCond   ${sections:%1|NOTFOUND}   !NOTFOUND   [nocase]
RewriteRule   ^section\.php$   ${sections:%1}?  [nocase,redirect=perm,last]

Using this set of rules, only uri's with mapped (query) keys would get redirected. 使用这组规则,只有带有映射(查询)键的uri才会被重定向。

Cheers. 干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM