我想从旧网站URL重定向到新网站URL。 我在htaccess文件中有很多规则。 请看下面的例子:通用重写规则将查询附加到htaccess中的所有定义的重写规则
RewriteRule ^blog/one$ http://www.newsite.com/blog/x/ [R=301,L]
RewriteRule ^blog/two$ http://www.newsite.com/blog/y/ [R=301,L]
RewriteRule ^blog/three$ http://www.newsite.com/blog/z/ [R=301,L]
and so on..........
重定向工作正常。 现在我想追加一个查询字符串“?key = value & anotherKey = anotherValue”到所有目标网址。 有没有办法通过单行代码来完成这项任务? 或者我需要将这个查询追加到所有的url。
这里是我想要一个例子:
RewriteRule ^blog/one$ http:www.newsite.com/blog/x/?key=value&anotherKey=anotherValue [R=301,L]
编辑: 完整的htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase/
#Custom redirection Starts form here
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^.*$ $0?key=value&anotherKey=anotherValue [L,NC]
#home page redirection
RewriteRule ^$ http://newsite.com/ [R=301,L]
#Blog Rewrite Rules
RewriteRule ^blog/this-blog-is-included-for-redirection/?$ http://newsite.com/blog/ [R=301,L]
#Custom redirection ends here
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
当我访问此网址: oldsite.com/blog/this-blog-is -included-for-redirection/
它重定向到: newsite.com/blog/?key=value & anotherK ey = anotherValue
这很好。
当我访问此网址: oldsite.com/blog/this-blog-is-exluded-for-redirection/
它重定向到: oldsite.com/blog/this-blog-is- exvded-for-redirection /?key = value & anotherKey = anotherValue
这里我不想附加查询。
规则工作。我想通过一行或几行代码将查询字符串附加到所有目标网址。有没有办法做到这一点? –
你可以这样做:http://www.newsite.com/blog/x/?param1 = x&param2 = y''[R = 301,L,QSA]' –
不是'^ blog/one'你可以用'/ block /(.*)'将所有博客重写为'.../blog/$ 1?key = ...'。问题是你有一个重定向,而不是重写,所以这可能会导致无限循环,所以你可能需要改进正则表达式或添加条件 – apokryfos