使用.htaccess和mod-rewrite匹配表达式临时重定向302

时间:2010-08-09 15:49:36

标签: regex .htaccess redirect http-status-code-302

我正在尝试为我的网站匹配一堆重定向,基本上移动到服务器上的不同文件夹。我需要http://www.site.com/index.php?page=anypage转到http://www.site.com/newfolder/index.php?page=anypage。事情是http://www.site.com/index.phphttp://www.site.com/index.php?page=home应保持不变。我怎么能做到这一点?

我在 .htaccess 文件中尝试以下操作,但我很害怕犯错误。我真的不知道如何测试这个。

Options +FollowSymlinks
RewriteEngine on

RewriteRule ^/index.php?page=(.*)$ http://www.site.com/newfolder/index.php?page=$1 [R=302,NC]
RewriteRule ^/index.php?page=home http://www.site.com/index.php?page=home [R=302,NC,L]

现在我认为这是暂时的,所以我应该知道要扭转它!下周,链接将不得不再次重定向到根服务器。另外,我该怎么做才能重新建立正常的重定向?

1 个答案:

答案 0 :(得分:3)

如果我正确地按照你的方案,你想要这样的东西:

RewriteEngine On

RewriteCond %{QUERY_STRING} !=""
RewriteCond %{QUERY_STRING} !page=home
RewriteRule ^index.php /newfolder/index.php [R,L]

就测试而言,我更喜欢在本地测试服务器上尝试规则。如果您可以完全控制服务器(就像在本地一样),那么有一些mod_rewrite指令可以帮助您记录正在进行的操作,这对调试很有帮助。 The module documentation有更多相关信息。

修改:当您想要切换回来时,请修改上面的RewriteRule,如下所示:

RewriteRule ^newfolder/index\.php /index.php [R,L]