首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >仅当存在查询字符串时,htaccess 301才重定向

仅当存在查询字符串时,htaccess 301才重定向
EN

Stack Overflow用户
提问于 2020-06-19 14:46:20
回答 1查看 56关注 0票数 0

如果存在查询字符串,是否有可能将重定向(最好是301重定向)包含到外部域?

代码语言:javascript
复制
Example:
domain.com => domain.com
domain.com?query-string => another-domain.com?query-string

非常感谢!

@anubhava非常感谢您的帮助。现在,我要补充一些附加条件:

代码语言:javascript
复制
first-domain.com => https://www.first-domain.com
www.first-domain.com => https://www.first-domain.com
http://first-domain.com => https://www.first-domain.com

first-domain.com?query-string => https://www.another-domain.com/first-domain/?query-string
www.first-domain.com?query-string => https://www.another-domain.com/first-domain/?query-string
http://first-domain.com?query-string => https://www.another-domain.com/first-domain/?query-string
https://www.first-domain.com?query-string => https://www.another-domain.com/first-domain/?query-string

second-domain.com?query-string => https://www.another-domain.com/second-domain/?query-string
www.second-domain.com?query-string => https://www.another-domain.com/second-domain/?query-string
http://second-domain.com?query-string => https://www.another-domain.com/second-domain/?query-string
https://www.second-domain.com?query-string => https://www.another-domain.com/second-domain/?query-string
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-19 15:12:26

您可以在旧域的站点根.htaccess中使用此规则:

代码语言:javascript
复制
RewriteEngine On

RewriteCond %{QUERY_STRING} .
RewriteCond %{HTTP_HOST} !^another-domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ http://another-domain.com/%1%{REQUEST_URI} [L,NE,R=301]

  • RewriteCond %{QUERY_STRING} .确保查询字符串存在。
  • 查询字符串将自动转发到目标URL.

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62472758

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档