HTTP到HTTPS通过的.htaccess

问题描述 投票:59回答:14

我已经通过现有的问题看,但我还没有真正遇到任何事情对我的作品。

我目前正在运行一个安全的SSL证书的站点。它可以在https://www.example.co.uk一个问题要访问的网站,也可以在http://www.example.co.uk访问 - 我不希望这是可能的。我需要它从HTTP重定向到https。

我发现这个代码片段一个在.htaccess文件中使用。

Options +FollowSymLinks 
RewriteEngine on 
RewriteCond %{HTTP_HOST} ^example.co.uk [NC] 
RewriteRule ^(.*)$ https://example.co.uk/$1 [L,R=301]

当用户example.co.uk进入他们的地址栏也能正常工作,但我还需要添加某种形式的条件语句,这样,如果用户输入“www.example.co.uk”或“http://www.example.co.uk”。

我已经使用[OR]的喜欢尝试,但这个最终建立服务器错误。

任何帮助和建议表示赞赏。

干杯。

http .htaccess redirect https
14个回答
158
投票

尝试以下方法:

 RewriteEngine On
 RewriteCond %{HTTPS} !=on
 RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

此外,您还可以根据端口号,例如重定向:

 RewriteCond %{SERVER_PORT} ^80$
 RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

这将重定向端口80上收到HTTPS的所有请求。


1
投票
# Switch rewrite engine off in case this was installed under HostPay.
RewriteEngine Off

SetEnv DEFAULT_PHP_VERSION 7

DirectoryIndex index.cgi index.php

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

# RewriteCond %{HTTP_HOST} ^compasscommunity.co.uk\.com$ [NC]
# RewriteRule ^(.*)$ https://www.compasscommunity.co.uk/$1 [L,R=301]

1
投票

由于这是在搜索结果的最前面的一个,如果你想添加HTTP到HTTPS的AWS豆茎重定向,接受的解决方案将无法工作。你需要下面的代码来代替:

RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^.*$ https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]

1
投票

试试这个RewriteCond %{HTTP_HOST} !^www. [NC] RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]


1
投票

完美的代码将HTML指数:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^YourNameWebsite\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.YourNameWebsite\.com$
RewriteRule ^/?$ "https\:\/\/YourNameWebsite\.com\/index\.html" [R=301,L]

要么

完美的代码将PHP指数:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^YourNameWebsite\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.YourNameWebsite\.com$
RewriteRule ^/?$ "https\:\/\/YourNameWebsite\.com\/index\.php" [R=301,L]

0
投票

这些都不为我工作,除了这一点。我的网站是在https://www.asmallorange.com托管

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

13
投票

添加以下代码在.htaccess文件。

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

改变example.com与您的网站域名

网址重定向教程可以从这里找到 - Redirect non-www to www & HTTP to HTTPS using .htaccess file


5
投票

试试这个,我用它,它工作正常

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

3
投票

尝试这个:

RewriteEngine On

RewriteCond %{HTTPS} off

RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

来源:https://www.ndchost.com/wiki/apache/redirect-http-to-https

(我试过的代码,这么多不同的区块,这3衬垫工作得十分完美)


3
投票

我尝试所有的上述代码,但任何代码不为我工作website.then我尝试此代码,该代码为我的网站上运行完美。您可以使用htaccess的以下规则:

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On

//Redirect http to https
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

//Redirect non-www to www
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

</IfModule>

更改example.com与您的域名和我的英文不好对不起。


1
投票

要重定向http://example.comhttp://www.example.com以简单的方式来https://www.example.com,您可以使用htaccess的以下规则:

RewriteEngine on

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

[测试]

%{} REQUEST_SCHEME变量可因为Apache 2.4,此变量包含请求的方案(HTTP或HTTPS),在Apache 2.4,你可以使用以下规则的价值:

RewriteEngine on


RewriteCond %{REQUEST_SCHEME} ^http$
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R]

1
投票

还有更好的,更安全的方式,以确保所有的流量都超过https。例如设置两个虚拟主机和重定向从http所有流量https主机。了解更多关于这方面this answer here on security.stackexchange.com

随着设置虚拟主机重定向可以发送301种状态(永久重定向),所以浏览器理解以下所有请求应被发送到它被重定向到HTTPS服务器。因此,没有进一步的http请求将第一重定向响应后进行。

你也应该仔细检查给出答案,因为有错误的重写规则设置,你可以从你的传入请求松散的查询参数。


1
投票

如果你想HTTP重定向到HTTPS,并希望与每个URL添加WWW,使用下面的htaccess的

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

它会先HTTP重定向到HTTPS,然后它会重定向到www。


1
投票

对我来说,工作只有这个变种:

RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

由于https://www.reg.ru/support/hosting-i-servery/sajty-i-domeny/kak-dobavit-redirekt/redirekt-s-http-na-https(俄文)

© www.soinside.com 2019 - 2024. All rights reserved.