简体   繁体   English

使 .htaccess 文件中的 https 重定向仅在线工作

[英]Make https redirect in .htaccess file work online only

I just created my first encrypted (https) site.我刚刚创建了我的第一个加密 (https) 站点。 I was advised that it might be best to use https in my "canonical" links and use my .htaccess file to redirect traffic to the new https links.有人建议我最好在我的“规范”链接中使用 https 并使用我的 .htaccess 文件将流量重定向到新的 https 链接。 I found the code for that task here .我在这里找到了该任务的代码。

RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

It works fine online.它在网上运行良好。 The problem is that it messes up my local sites.问题是它弄乱了我的本地站点。 It isn't a huge problem;这不是一个大问题。 I could simply make separate .htaccess files - one for online and one for local use.我可以简单地制作单独的 .htaccess 文件 - 一个供在线使用,一个供本地使用。 But that would be a minor pain, so I wondered if there's another solution.但这将是一个小痛苦,所以我想知道是否有其他解决方案。

My local pages won't display at https, so I wondered if there's a way to tell my .htaccess file to make this new rule apply online only.我的本地页面不会在 https 上显示,所以我想知道是否有办法告诉我的 .htaccess 文件让这个新规则只在线应用。

EDIT:编辑:

Maybe I should reword my post to ask how YOU handle this situation.也许我应该改写我的帖子,询问您如何处理这种情况。 I'm not even sure if .htaccess is the only player here...我什至不确定 .htaccess 是否是这里唯一的玩家......

  1. don't use the .htaccess file on your local host.不要在本地主机上使用.htaccess文件。
  2. encrypt your local host.加密您的本地主机。 it's really easy to make a self-signed certificate with openssl for free.使用openssl免费制作自签名证书真的很容易。

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

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