简体   繁体   English

.htaccess无法将URL重写为HTTPS

[英].htaccess not rewriting URL to HTTPS

I have this code in my .htaccess to rewrite my URL from http://123domain.com to https://www.123domain.com but it's not working? 我的.htaccess文件中有此代码,可以将我的URL从http://123domain.com重写为https://www.123domain.com,但不能正常工作吗?

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

The internet is full of solutions for this. 互联网上有很多解决方案。 None of all those examples answered your question? 所有这些示例都没有回答您的问题?

You cannot do both rewritings in a single step in a reliable manner. 您不能以可靠的方式在一个步骤中完成两次重写。 Instead you need two steps: 相反,您需要两个步骤:

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [END,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [END,R=301]

Note: in case you are using a very old http server version you have to replace the END flags with L flags, should work the same in this case. 注意:如果您使用的是非常旧的http服务器版本,则必须将L标志替换为END标志,在这种情况下应该可以正常工作。


And a general hint: you should always prefer to place such rules inside the http servers host configuration instead of using dynamic configuration files (".htaccess"). 还有一个一般性提示:您应该始终喜欢将此类规则放置在http服务器主机配置中,而不是使用动态配置文件(“ .htaccess”)。 Those files are notoriously error prone, hard to debug and they really slow down the server. 众所周知,这些文件容易出错,难以调试,并且确实降低了服务器的速度。 They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare). 仅当您无法控制主机配置(阅读:真正便宜的托管服务提供商)或者您的应用程序依赖于编写自己的重写规则(这显然是安全噩梦)时,才提供它们作为最后的选择。 )。

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

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