简体   繁体   English

将查询附加到 htaccess 中所有定义的重写规则的通用重写规则

[英]Common rewrite rule to append query to all the defined rewrite rules in htaccess

I want to redirect from old site urls to new site urls.我想从旧网站网址重定向到新网站网址。 I have many rules in htaccess files.我在 htaccess 文件中有很多规则。 See example below:请参阅下面的示例:

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..........

Redirection is working fine.重定向工作正常。 Now I want to append a query string "?key=value&anotherKey=anotherValue" to all the destination urls.现在我想将查询字符串“?key=value&anotherKey=anotherValue”附加到所有目标网址。 Is there a way to do this task by single line of code?有没有办法通过单行代码完成这项任务? Or do I need to append this query to all the urls one by one.或者我是否需要将此查询一一附加到所有网址。

Here is an example that I want:这是我想要的一个例子:

RewriteRule ^blog/one$ http:www.newsite.com/blog/x/?key=value&anotherKey=anotherValue [R=301,L]

EDITED: complete htaccess:编辑:完整的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>

When I access this url: oldsite.com/blog/this-blog-is-included-for-redirection/当我访问这个网址时:oldsite.com/blog/this-blog-is-included-for-redirection/

it redirects to: newsite.com/blog/?key=value&anotherKey=anotherValue它重定向到:newsite.com/blog/?key=value&anotherKey=anotherValue

This is fine.这可以。

When I access this URL: oldsite.com/blog/this-blog-is-exluded-for-redirection/当我访问这个 URL 时:oldsite.com/blog/this-blog-is-exluded-for-redirection/

it redirects to: oldsite.com/blog/this-blog-is-exluded-for-redirection/?key=value&anotherKey=anotherValue它重定向到:oldsite.com/blog/this-blog-is-exluded-for-redirection/?key=value&anotherKey=anotherValue

Here I do not want to append the query.在这里我不想追加查询。

You can keep a generic rule before your redirect rules to set the query string and redirect rules will automatically carry over query string to target URIs:您可以在重定向规则之前保留一个通用规则来设置查询字符串,重定向规则会自动将查询字符串转移到目标 URI:

RewriteEngine On

# generic rule for query string
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^blog/(?:one|two|three)/?$ $0?key=value&anotherKey=anotherValue [L,NC]

RewriteRule ^blog/one/?$ http://www.newsite.com/blog/x/ [R=301,L,NC]
RewriteRule ^blog/two/?$ http://www.newsite.com/blog/y/ [R=301,L,nC]
RewriteRule ^blog/three/?$ http://www.newsite.com/blog/z/ [R=301,L,NC]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM