简体   繁体   English

.htaccess使用GET参数和URL条件重定向

[英].htaccess redirect with GET parameters and URL condition

I try to redirect to another domain and changing the URL structure (not keeping URI part) based on 2 conditions: 我尝试根据2个条件重定向到另一个域并更改URL结构(不保留URI部分):

  1. The original URL has "lang" parameter. 原始网址具有“ lang”参数。

  2. The original URI begins with "page". 原始URI以“页面”开头。

It's Apache 2.2 是Apache 2.2

This is working when only have the lang paramenter in the orginal URL: 当原始URL中仅具有lang paramenter时,此方法有效:

RewriteCond %{QUERY_STRING} lang=en$
RewriteRule ^.*$ https://www.destination.com/en/? [R=301,L]

For example it redirects ok for: 例如,它将ok重定向为:

http://www.origin.com/?lang=en

to: 至:

http://www.destination.com/en/

But I also need to redirect something like: 但我还需要重定向以下内容:

http://www.origin.com/page/5/?lang=en

to: 至:

http://www.destination.com/en/

I'm trying things like this, but doesn't work, I think there's something wrong in the RewriteRule pattern: 我正在尝试类似的操作,但不起作用,我认为RewriteRule模式中存在错误:

RewriteCond %{QUERY_STRING} ^lang=en$
RewriteRule ^/page/.*/?$ https://www.destination.com/en/? [R=301,L]

Welcome! 欢迎!

You may not want to do have a ? 您可能不想拥有一个? at the end of your RewriteRule. 在RewriteRule的末尾。 However, if you wish to have you can do so. 但是,如果您愿意,可以这样做。 Maybe, you want to have a RewriteRule similar to: 也许您想拥有一个类似于以下内容的RewriteRule:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{QUERY_STRING} lang=en$
    RewriteRule (.*)(\?lang=en) https://www.destination.com/en/ [R=301,L]

</IfModule>
  • You might want to restart your apache, with a command similar to: 您可能要使用类似以下的命令来重新启动apache:

     sudo apachectl restart 
  • You might clear your browser cache every time that you make a change on .htaccess file 每次对.htaccess文件进行更改时,您可能都会清除浏览器缓存

在此处输入图片说明

Graph 图形

This graph shows how your expression works, if you wish to know: 该图显示了表达式的工作原理,如果您想知道:

在此处输入图片说明

This tool can help you to simply design any rule that you like to have. 该工具可以帮助您简单地设计自己想要的任何规则。

I have assumed that you want to redirect all your ?lang=en containing ULRs to a single URL. 我假设您要将所有包含ULR的?lang=en重定向到单个URL。 I was not sure about that. 我不确定。 If that's not the case, you may not use my expression. 如果不是这种情况,则您可能无法使用我的表达方式。

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

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