当一个查询字符串中显示两个“关键字”时,RewriteRule / RewriteCond会提供帮助

时间:2014-04-21 17:45:47

标签: apache .htaccess mod-rewrite rewrite

目前我在.htaccess文件中有此内容:

<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /forum/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(jpeg|jpg|gif|png)$ /forum/public/404.php [NC,L]

RewriteCond %{QUERY_STRING} (?:^|&)app=core(?:&|$) [NC]
RewriteCond %{QUERY_STRING} (?:^|&)section=register(?:&|$) [NC]
RewriteRule ^index\.php$ http://www.mysite.com/register.php? [NC,L,R=302]

RewriteCond %{QUERY_STRING} (?:^|&)app=core(?:&|$) [NC]
RewriteCond %{QUERY_STRING} (?:^|&)section=login(?:&|$) [NC]
RewriteRule ^index\.php$ http://www.mysite.com/login.php? [NC,L,R=302]

RewriteCond %{QUERY_STRING} (?:^|&)app=core(?:&|$) [NC]
RewriteCond %{QUERY_STRING} (?:^|&)section=lostpass(?:&|$) [NC]
RewriteRule ^index\.php$ http://www.mysite.com/retrieve_password.php? [NC,L,R=302]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /forum/index.php [L]
</IfModule>

我需要帮助的是这一个:

RewriteCond %{QUERY_STRING} (?:^|&)app=core(?:&|$) [NC]
RewriteCond %{QUERY_STRING} (?:^|&)section=login(?:&|$) [NC]
RewriteRule ^index\.php$ http://www.mysite.com/login.php? [NC,L,R=302]

目前,这将重定向:

http://www.mysite.com/forum/index.php?app=core&module=global&section=login

&module=global不是必需的,因此我确信您只能查找app&amp; section参数。

然而问题是,它还重定向我要重定向到其他地方的注销链接,因为它还包含app=core&amp; &section=login

以下是一个例子:

http://www.mysite.com/forum/index.php?app=coree&module=global&section=login&do=logout&k=xxxxxxxxxxxxxxxxxxx

所以基本上登录仍然应该像一样重定向,除非在查询字符串中有&do=logout;在这种情况下,我想将其重定向到其他地方。

我该怎么做呢?

1 个答案:

答案 0 :(得分:1)

将您的规则更改为:

RewriteCond %{QUERY_STRING} (?:^|&)app=core(?:&|$) [NC]
RewriteCond %{QUERY_STRING} (?:^|&)section=login(?:&|$) [NC]
RewriteCond %{QUERY_STRING} !(?:^|&)do=logout(?:&|$) [NC]
RewriteRule ^index\.php$ http://www.mysite.com/login.php? [NC,L,R=302]

如果查询字符串中存在RewriteCond %{QUERY_STRING} !(?:^|&)do=logout(?:&|$) [NC]&do=logout将跳过重定向。

编辑:根据评论:

RewriteCond %{QUERY_STRING} (?:^|&)app=core(?:&|$) [NC]
RewriteCond %{QUERY_STRING} (?:^|&)section=login(?:&|$) [NC]
RewriteCond %{QUERY_STRING} (?:^|&)do=logout(?:&|$) [NC]
RewriteRule ^index\.php$ http://www.mysite.com/logout.php? [NC,L,R=302]