htaccess 301将查询重定向到新域

时间:2013-07-29 12:30:18

标签: .htaccess redirect

尝试将此查询重定向到新域:

www.domain.com/search.php?q=keyword

www.newdomain.com/search.php?q=keyword

关键字可以是任何单词

并希望将其余内容保留在域中,我只需要重定向此查询。 我尝试了几种方法,而我发现更接近解决方案的是这一种:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^q=1$
RewriteRule ^search\.php$ http://www.newdomain.com/search.php?q=$1 [R=301,L]

但不起作用! 希望有人可以帮助我纠正这个问题,如果这是最好的工作方式吗? 非常感谢。

3 个答案:

答案 0 :(得分:1)

试试这个

RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=123$
RewriteRule ^/?product\.php$ http://website.com.au/product_123.php? [L,R=301]

RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=([^&]+)
RewriteRule ^/?product\.php$ http://website.com.au/product_%1.php? [L,R=301]

答案 1 :(得分:1)

你非常接近。

通过httpd.conf启用mod_rewrite和.htaccess,然后将此代码放在.htaccess目录下的DOCUMENT_ROOT中:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^(www.)?domain\.com$ [NC]
RewriteCond %{QUERY_STRING} ^q=.+ [NC]
RewriteRule ^(search\.php)$ http://www.newdomain.com/$1 [R=301,L,NC]

答案 2 :(得分:0)

这很有效,很简单

RewriteEngine on
RewriteRule ^(.*)$ http://newdomain.com/$1 [QSA]
相关问题