简体   繁体   English

apache 将 http 重定向到 https,将 www 重定向到非 www

[英]apache redirect http to https and www to non www

basically what i want is redirect al request to use HTTPS instead of http基本上我想要的是重定向所有请求以使用 HTTPS 而不是 http

I have this in my htaccess so far and it worked great: Code:到目前为止,我的 htaccess 中有这个,而且效果很好:代码:

<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteCond %{HTTPS} off 
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} 
</ifModule> 

today someone noticed that when going to: http://www.example.com it redirects to and shows an unsecure connection thingie.今天有人注意到,在访问: http : //www.example.com 时,它重定向到并显示了一个不安全的连接东西。

My ssl is setup for non www domain: mydomain.com我的 ssl 是为非 www 域设置的:mydomain.com

So i need to make sure all site requests are sent to non www and https: It works fine if i put example.com it redirects to https://example.com所以我需要确保所有站点请求都发送到非 www 和 https:如果我把 example.com 重定向到https://example.com,它工作正常

but with www.example.com it goes to htts://www.example.com and shows the error但是使用 www.example.com 它会转到 htts://www.example.com 并显示错误

what do i need to add to my code to redirect www to non www and then to ssl ?我需要在代码中添加什么才能将 www 重定向到非 www 然后重定向到 ssl?

You will have to re-issue your certificate for both www and without www. 您将不得不为www和没有www重新颁发证书。

If someone connects to your site via a domain name that is not included in your common name, they will receive a warning. 如果有人通过您的公用名中未包含的域名连接到您的网站,他们将收到警告。

The ssl negociation process happens before any response from the server (in your case, a redirection), so in all cases, your visitors will receive a warning when using a domain that is not in your common name. ssl negociation进程在服务器的任何响应之前发生(在您的情况下,重定向),因此在所有情况下,当您使用不在您的公用名中的域时,您的访问者将收到警告。

You can get what you need from the HTTP_HOST 您可以从HTTP_HOST获得所需内容

RewriteEngine On 
RewriteCond %{HTTPS} off 
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
RewriteRule (.*) https://%1%{REQUEST_URI} [L,R=301]

This way it will get the host always without the subdomain. 这样它将使主机始终没有子域。

RewriteEngine On 
RewriteCond %{HTTPS} off
RewriteRule (.*) https://domain.com%{REQUEST_URI} [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^www.domain.com [NC]
RewriteRule (.*) https://domain.com%{REQUEST_URI} [L,R=301,NC]

If you are using CloudFlare's free account then that's the problem. 如果您使用的是CloudFlare的免费帐户,那就是问题所在。 CloudFlare's free account does NOT support SSL Certificates. CloudFlare的免费帐户不支持SSL证书。 To continue using CloudFlare's free account with an SSL Certificate just go to the DNS settings in CloudFlare and take the orange cloud off of your domain and off of the cname WWW. 要继续使用带有SSL证书的CloudFlare免费帐户,只需转到CloudFlare中的DNS设置,并从您的域中取出橙色云,然后从cname WWW中取出。 That will fix your problem and cause both www and non-www to be redirected to https. 这将解决您的问题,并导致www和非www重定向到https。

Also be sure to add this code to your .htaccess file: 另外一定要将此代码添加到.htaccess文件中:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Then, everything should work! 然后,一切都应该工作!

RewriteEngine On 
RewriteCond %{HTTPS} off 
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
RewriteRule (.*) https://www.%1%{REQUEST_URI} [L,R=301]

Check out this: 看看这个:

RewriteEngine On
RewriteCond %{HTTP_HOST}#%{HTTPS}s ^www\.([^#]+)#(?:off|on(s)) [NC]
RewriteRule ^ http%2://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

This will redirect all of your www websites to non-www and secure them if you have completed the CERTBOT for each domain conf file.如果您已完成每个域 conf 文件的 CERTBOT,这会将您的所有 www 网站重定向到非 www 并保护它们。 Put this in /etc/apache2/apache2.conf inside the Directory /www section:把它放在目录 /www 部分的 /etc/apache2/apache2.conf 中:

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

There is no need to CERTBOT a www domain after this code is inserted.插入此代码后,无需 CERTBOT www 域。 Just do the domain.com choice.只需选择 domain.com 即可。 You do not need htaccess files.您不需要 htaccess 文件。 They can be restricted by the AllowOverride None selection.它们可以通过 AllowOverride None 选项进行限制。

Remember to restart apache.记得重启apache。

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

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