Apache 在htaccess中强制使用https

Apache 在htaccess中强制使用https,apache,.htaccess,mod-rewrite,https,Apache,.htaccess,Mod Rewrite,Https,我得到了我的站点的证书,现在我需要将其移动到https。这是我的htaccess: # Turn on URL rewriting RewriteEngine On # Installation directory RewriteBase / # Protect hidden files from being viewed <Files .*> Order Deny,Allow Deny From All </Files> # Protect app

我得到了我的站点的证书,现在我需要将其移动到https。这是我的htaccess:

# Turn on URL rewriting
RewriteEngine On

# Installation directory
RewriteBase /

# Protect hidden files from being viewed
<Files .*>
    Order Deny,Allow
    Deny From All
</Files>

# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]

# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]
#启用URL重写
重新启动发动机
#安装目录
重写基/
#保护隐藏文件不被查看
命令拒绝,允许
全盘否定
#保护应用程序和系统文件不被查看
重写规则^(?:应用程序|模块|系统)\b.*index.php/$0[L]
#允许直接显示现有的任何文件或目录
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
#将所有其他URL重写为index.php/URL
重写规则。*index.php/$0[PT]

我该怎么做?谢谢

有几种方法可以做到这一点;这里有一个:

## port requirement (bail if not 443)
RewriteCond %{SERVER_PORT} !^443$
## exceptions
RewriteCond %{REQUEST_URI} ^/somepath$ [OR]
RewriteCond %{REQUEST_URI} ^/anotherpath$
## force traffic to https equivalent
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

我添加了2个可选的RewriteCond条目,允许您根据URI的第一个组件指定异常。

这已经被问了很多次了。感谢您的帮助,但我想知道如何将代码放入现有的代码中。可能就在本节之后(非常确定这就是它的位置)。实验,试着去掉可选部分(尽管它们不重要),您可能需要尝试第一个RewriteCond(我已经得到了与apache等不同版本的混合结果),另一个选项是
%{SERVER_PROTOCOL}^HTTPS$
,网上有一百万个例子;我从一个实时服务器中提取了这个示例代码,这样它就可以工作了;只需启用和禁用带有注释的部分,直到您找到真正适合您的内容。谢谢。。。好吧,我把这个问题贴在这里,这样我就不必去试验和谷歌了,但我想我终究还是得这么做:)你有Apache在80和443上监听吗?您需要捕获端口80上的请求以重定向到443。您应该有两个vhost指向同一webroot。