简体   繁体   English

尝试将没有查询字符串的任何请求重定向到我的域到子域

[英]Trying to redirect any requests to my domain without query strings to a subdomain

Using .htaccess , I'm trying to find out how to redirect any requests to my site base URL / domain but to allow any requests with query strings, and if possible, only certain prepended query strings.使用.htaccess ,我试图找出如何将任何请求重定向到我的站点基础 URL / 域,但允许任何带有查询字符串的请求,如果可能的话,只允许某些前置查询字符串。 For example:例如:

  • Request to / is redirected to blog.example.com//的请求被重定向到blog.example.com/
  • Request to /?anything (even without = ) will be served as usual/?anything的请求(即使没有= )将照常处理

Bonus points if anyone can describe how all pages would be redirected unless only containing certain query strings.如果任何人都可以描述如何重定向所有页面,除非只包含某些查询字符串,那么奖励积分。

You can use this rewrite rule:您可以使用此重写规则:

RewriteEngine on
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^\?$ https://blog.example.com/ [R=301,L]
  • The condition with %{QUERY_STRING} ^$ ensures that the query string is empty (starts with symbol followed by ends with symbol.)带有%{QUERY_STRING} ^$的条件确保查询字符串为空(以符号开头,然后以符号结尾。)
  • The rule ensures that the path contains at most a single slash and causes the redirect.该规则确保路径最多包含一个斜杠并导致重定向。

If you want to redirect everything except a specific query string, you can use a very similar rule.如果您想重定向除特定查询字符串之外的所有内容,您可以使用非常相似的规则。 This one will redirect any query string on the root path except /?foo=bar这将重定向根路径上的任何查询字符串,除了/?foo=bar

RewriteEngine on
RewriteCond %{QUERY_STRING} !^foo=bar$
RewriteRule ^\?$ https://blog.example.com/ [R=301,L]

No matter how many rewrite rules you have, you only need one instance of RewriteEngine on in your .htaccess .无论您有多少重写规则,您只需要在.htaccess中打开一个RewriteEngine on实例。

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

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