重新写入和重写www。重定向和HTTPS重定向

发布于 2025-02-13 10:07:42 字数 1267 浏览 0 评论 0原文

我发现按照重写规则,我想结合使用。它们存储在/etc/apache2/sites-enabled/000-default.conf中,

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]

不幸的是它没有一起工作,其中只有一个正在工作。

例如: 如果我打开 http://example.com/ 它将我重定向到 https://example.com/ 如果我打开 https://www.example.com/ 它将我重定向到 https://example.com/

到目前为止,一切都很好。

但是,如果我打开 http://www.example.com/ ...它无济于事。

为什么?

编辑:

    <If "%{HTTP_HOST} == '^www\.example\.com$'">
      RewriteRule ^(.*)$ https://example.com/$1 [NC,R=301,L]
    </If>
    <If "%{SERVER_PORT} != '^443$'">
      RewriteRule (.*) https://%{HTTP_HOST}/$1 [NC,R=301,L]
    </If>

也不起作用。

i found following Rewrite Rules, i would like to combine. They are stored in the /etc/apache2/sites-enabled/000-default.conf

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]

Unfortunately it is not working together, only one of them is working.

For example:
If i open http://example.com/ it redirects me to https://example.com/
If i open https://www.example.com/ it redirects me to https://example.com/

So far, so good.

But if i open http://www.example.com/ ... it does nothing.

Why?

Edit:

    <If "%{HTTP_HOST} == '^www\.example\.com

Also is not working.

"> RewriteRule ^(.*)$ https://example.com/$1 [NC,R=301,L] </If> <If "%{SERVER_PORT} != '^443

Also is not working.

"> RewriteRule (.*) https://%{HTTP_HOST}/$1 [NC,R=301,L] </If>

Also is not working.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文