使用参数将 URL 重定向到顶级域 [英] Redirecting URL with paramter to top-level domain
问题描述
我一直在尝试搜索,但还没有运气.找到了有关重定向的不同选项,但与我正在寻找的完全不同.
I have been trying to search but no luck yet. Found different options about redirecting but nothing like what I'm looking for.
所以目前网站 URL 包含 lang
参数,如 ?lang=en
、?lang=ru
、?lang=fi代码>.参数位于 URL 的最后.
So currently the website URLs contain lang
parameter like ?lang=en
, ?lang=ru
, ?lang=fi
. Parameters are on the very end of the URL.
想法是将语言移至顶级域.所以基本上我正在寻找一种方法来将包含参数 ?lang=ru
的所有 URL 重定向到顶级 .ru
域.与其他语言相同.
Idea is to move languages to top level domain. So basically I'm looking for a way to redirect all URLs that contain parameter ?lang=ru
to top-level .ru
domain. Same with other languages.
我可以通过 .htaccess
来做还是根本不应该做?将站点移动到不同的域应该需要重定向才能将链接汁和权限传递给新域.
Can I do it via .htaccess
or it shouldn't be done at all? Moving site to different domains should need redirection to pass the link juice and authority to new domains.
希望有人能引导我找到正确的方法.
Hopefully, someone can lead me to the correct way of doing it.
非常感谢!
推荐答案
如果没有其他需要保留的 URL 参数,那么您可以使用 .htaccess<顶部附近的 mod_rewrite 执行如下操作/code> 文件:
If there are no other URL parameters that need to be preserved then you can do it like the following using mod_rewrite near the top of your .htaccess
file:
RewriteEngine On
# Redirect "/foo?lang=xx" to "example.xx/foo"
RewriteCond %{QUERY_STRING} ^lang=([a-z]{2})$
RewriteRule ^ https://example.%1%{REQUEST_URI} [QSD,R=302,L]
以上匹配任何由 2 个小写字母组成的语言代码.要匹配特定的语言代码,请在正则表达式中使用 alternation 并将 RewriteCond
指令更改为:
The above matches any language code that consists of 2 lowercase letters. To match specific language codes then use alternation in the regex and change the RewriteCond
directive to read:
RewriteCond %{QUERY_STRING} ^lang=(en|ru|fi|abc|etc)$
QSD
标志会丢弃重定向响应 (Apache 2.4) 中的原始 lang=xx
查询字符串.否则,这将默认复制到目标 URL 上.
The QSD
flag discards the original lang=xx
query string from the redirect response (Apache 2.4). Otherwise this will be copied onto the target URL by default.
%1
反向引用包含在前面的条件中捕获的 lang
URL 参数的值.REQUEST_URI
服务器变量包含来自请求的完整 URL 路径(无查询字符串).
The %1
backreference contains the value of the lang
URL parameter captured in the preceding condition. The REQUEST_URI
server variable contains the full URL-path (no query string) from the request.
这确实假设特定语言的 TLD 域托管在其他地方.换句话说,我们不需要检查我们是否已经在所需的 TLD 域中.
This does assume that the language specific TLD domains are hosted elsewhere. In other words, we do not need to check whether we are already at the required TLD domain.
使用 302(临时)重定向进行测试,并且仅更改为 301(永久)重定向 - 如果这是您的意图 - 一旦您确认这可以正常工作.
Test with a 302 (temporary) redirect and only change to a 301 (permanent) redirect - if that is the intention - once you have confirmed that this works OK.
更新:任何特定的重定向,例如.lang=en
到 .com
需要先出现.例如:
UPDATE: Any specific redirects, eg. lang=en
to .com
will need to appear first. For example:
# Redirect languages to .com
RewriteCond %{QUERY_STRING} ^lang=(en)$
RewriteRule ^ https://example.com%{REQUEST_URI} [QSD,R=302,L]
# All other language codes...
# Redirect "/foo?lang=xx" to "example.xx/foo"
RewriteCond %{QUERY_STRING} ^lang=([a-z]{2})$
RewriteRule ^ https://example.%1%{REQUEST_URI} [QSD,R=302,L]
如果有更多语言代码,请使用alternation(如上所述).例如.(en|gb)
.
Use alternation (as mentioned above) if there are more language codes. eg. (en|gb)
.
这篇关于使用参数将 URL 重定向到顶级域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!