【问题标题】:How to create RewriteRule for the specific match https and www together?如何为特定匹配 https 和 www 创建 RewriteRule?
【发布时间】:2019-10-03 16:02:10
【问题描述】:

我已经设法为 www 和非 www(纯 http)和 https 创建了有效的重定向,但是当它们一起使用时,它不再起作用了。我有四个域,希望每个域都重定向到一个基域,然后添加 url 参数。

现在尝试不同变体时的当前结果:

www.example.fi -> redirects me to https://example.se/?lang=fi. OK
http://example.fi -> redirects me to https://example.se/?lang=fi OK
https://example.fi -> redirects me to https://example.se/?lang=fi OK
https://www.example.fi -> redirects me to https://example.se without the lang parameters.  WRONG. It should take me to the same place as the other.

这是我的 http 虚拟主机:

<VirtualHost *:80>
ServerName www.example.fi
ServerAlias example.fi
DocumentRoot /var/www/html/dir
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Redirect permanent / https://example.se/?lang=fi
</VirtualHost>

这是我的 https:

<VirtualHost *:443>
ServerName www.example.fi
ServerAlias example.fi
DocumentRoot /var/www/html/dir
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLEngine on
SSLCertificateFile /home/user/example.fi.pem
SSLCertificateKeyFile /home/user/example.fi.key
SSLCACertificateFile /home/user/intermediate.pem
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.fi$ [NC]
RewriteRule ^ https://example.se%{REQUEST_URI}/\?lang=fi [R=301,L]

如您所见,我希望不同的语言域使用语言参数指向 SE 域。

如何修改我的代码,使其与 https、http 和 www 的所有可能变体匹配?

【问题讨论】:

    标签: apache ssl url-rewriting virtualhost


    【解决方案1】:

    在你的代码中

      RewriteCond %{HTTP_HOST} ^example\.fi$ [NC]
      RewriteRule ^ https://example.se%{REQUEST_URI}/\?lang=fi [R=301,L]
    

    重写仅匹配 htt(s)://exemple.fi URL

    没有 ^ 它应该可以工作

      RewriteCond %{HTTP_HOST} example\.fi$ [NC]
      RewriteRule ^ https://example.se%{REQUEST_URI}/\?lang=fi [R=301,L]
    

    【讨论】:

    • 非常感谢!解决了我的大部分问题。但不幸的是,现在我有了一个新问题。可能是一个简单的解决方案,但如果我现在转到 example.fi/test,我会被重定向到 example.se/?lang=fitest。如何确保我的 /test/ 在 lang 参数之前获取?
    • 我认为这是因为您的 virtualHost 中的 RedirectPermeanent ,您可以尝试将其更改为 Redirect Permanent / example.se
    • 这并没有解决问题,但我在 :80 中添加了与 :443 相同的重写规则,但我添加了 RewriteCond %{HTTPS} 关闭。这是不好的做法吗?它似乎在大多数情况下都有效,但有时我会因为未知原因退回到普通的 .se 域。
    • 对我来说不是一个坏习惯对我来说 rewriterule 比 Redirect Permanent 更有用(你可以使用 %{REQUEST_URI} )
    猜你喜欢
    • 1970-01-01
    • 2016-06-27
    • 2020-05-22
    • 2017-07-31
    • 1970-01-01
    • 2016-09-03
    • 2016-06-15
    • 2014-12-16
    • 1970-01-01
    相关资源
    最近更新 更多