mod_rewrite apache 的问题 |将非 www 网址重定向到 www 网址

发布于 2025-01-03 00:35:39 字数 892 浏览 4 评论 0原文

我试图将我网站上的所有请求重定向到以下格式的 url:

https://www.mydomain.com 

因为我有 www.mydomain.com 的 SSL 证书。我在我的 httpd.conf 文件中编写了以下重写规则来执行此操作:

RewriteCond %{HTTP_HOST} !^www [NC]
RewriteRule $ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R]

RewriteCond %{HTTPS} off
RewriteRule $ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]

在以下可能的格式中,只有一种不起作用:

mydomain.com - working
www.mydomain.com - working
http://mydomain.com - working
https://mydomain.com – not working
https://www.mydomain.com - working
http://www.mydomain.com - working

任何人都可以解释此问题背后的原因和解决方案吗?

更新:

当我尝试访问不起作用的 URL (https://mydomain.com) 时,我收到无效的 SSL 证书错误(因为我的证书有效)对于 www.mydomain.com 而不是 mydomain.com),因此我的重写规则没有被执行,因为尚未建立 http 连接。

这个问题有什么解决办法吗?

I am trying to redirect all request on my site to url of format:

https://www.mydomain.com 

as I have SSL certificate for www.mydomain.com. I have written following rewrite rules in my httpd.conf file to do this:

RewriteCond %{HTTP_HOST} !^www [NC]
RewriteRule $ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R]

RewriteCond %{HTTPS} off
RewriteRule $ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]

Out of the following possible formats, only one is not working:

mydomain.com - working
www.mydomain.com - working
http://mydomain.com - working
https://mydomain.com – not working
https://www.mydomain.com - working
http://www.mydomain.com - working

Can anyone explain the reason behind and solution for this issue?

Update:

When I try to hit the URL which is not working (https://mydomain.com), I get an invalid SSL certificate error (as my certificate is valid for www.mydomain.com and not mydomain.com) and hence none of my Rewrite rule is getting executed as http connection is not yet made.

Is there any solution for this issue?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

快乐很简单 2025-01-10 00:35:39

你可能想尝试

rewritecond %{SERVER_PORT} !^443$

而不是

RewriteCond %{HTTPS} off

you may want to try

rewritecond %{SERVER_PORT} !^443$

instead of

RewriteCond %{HTTPS} off
撩人痒 2025-01-10 00:35:39

以我拥有的证书这是不可能的。尝试https://amazon.com,您将看到不受信任的连接问题。我有类似的问题。选择是购买另一个证书。

This is not possible with the certificate that I have. Try https://amazon.com and you will see the untrusted connection problem. I have similar issue. Option is to buy another certificate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文