使用htaccess重定向

时间:2013-03-22 20:42:06

标签: .htaccess mod-rewrite

这似乎是一项非常简单的任务,但在阅读了太多教程之后,我几个小时都无法完成。请帮忙..

我需要重定向

http://example.com/myfolder/myfile.php?type=1&add=20

到这个地址:

http://example.com/newfolder/mytasks.xml

我尝试了太多。我的最后一次试用是这样的:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^/myfolder/myfile.php?type=1&add=20$ /newfolder/mytasks.xml [R=301,NC,L]
</IfModule>

2 个答案:

答案 0 :(得分:1)

请改用此规则:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+myfolder/myfile\.php\?type=1&add=20 [NC]
RewriteRule ^ /newfolder/mytasks.xml [R=301,L]

记住:

  • RewriteRule匹配不以斜杠/
  • 开头
  • RewriteRule与查询字符串不匹配,仅匹配请求URI

答案 1 :(得分:1)

如果您还希望删除查询type=1&add=20,则此类内容应该有效:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING}  type=1&add=20                 [NC]
RewriteCond %{REQUEST_URI}   !/newfolder/                  [NC]
RewriteRule ^myfolder/myfile\.php  /newfolder/mytasks.xml? [R=301,NC,L]

重定向:

http://example.com/myfolder/myfile.php?type=1&add=20

http://example.com/newfolder/mytasks.xml

对于静音映射,将[R = 301,NC,L]替换为[NC,L]