wordpress 使用 .htaccess 将 HTTP 重定向到 HTTPS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32049820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:08:30  来源:igfitidea点击:

Use .htaccess to redirect HTTP to HTTPs

wordpress.htaccesshttpredirecthttps

提问by Cristiano Maia

Please, don't recommend me the long and very detailed threadwith more than 173 upvotes. It didn't work for me. I have also tried many others (1, 2, 3, 4). They all give me TOO_MANY_REDIRECTS or error 500. So, here is my issue:

请不要向我推荐超过 173 票的冗长且非常详细的帖子。它对我不起作用。我还尝试了许多其他方法(1234)。他们都给我 TOO_MANY_REDIRECTS 或错误 500。所以,这是我的问题:

With my current .htaccess, this is what happens:

使用我当前的 .htaccess,会发生以下情况:

https://www.dukescasino.com/- works perfectly

https://www.dukescasino.com/- 完美运行

https://dukescasino.com/- redirects to the above which is great

https://dukescasino.com/- 重定向到上面很棒

The two options below loads fine, but it should be redirecting to the https version:

下面的两个选项加载正常,但它应该重定向到 https 版本:

http://www.dukescasino.com/

http://www.dukescasino.com/

http://dukescasino.com/

http://dukescasino.com/

Here is the current .htaccess:

这是当前的 .htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

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

I don't believe it is relevant, but if so, here is the list of current active plugins:

我不相信它是相关的,但如果是这样,这里是当前活动插件的列表:

  • Advanced Custom Fields
  • All In One SEO Pack
  • Bop Search Box Item Type For Nav Menus
  • Contact Form 7
  • Disable Comments
  • Google XML Sitemaps
  • Jetpack by WordPress.com
  • Search & Filter
  • Slider WD
  • TablePress
  • UpdraftPlus - Backup/Restore
  • Wordfence Security
  • WPide
  • WP Smush
  • WP Super Cache
  • 高级自定义字段
  • 多合一搜索引擎优化包
  • 导航菜单的 Bop 搜索框项目类型
  • 联系表格 7
  • 禁用评论
  • 谷歌 XML 站点地图
  • Jetpack 由 WordPress.com 提供
  • 搜索和过滤
  • 滑块WD
  • 平板压力机
  • UpdraftPlus - 备份/恢复
  • 文字围栏安全
  • WPide
  • 可湿性粉剂
  • WP 超级缓存

Edit 1 - Tests performed:

编辑 1 - 执行的测试:

Test A:

测试A:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

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]

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

Result:ERR_TOO_MANY_REDIRECTS

结果:ERR_TOO_MANY_REDIRECTS

Test B:

测试乙:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

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

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

Result:ERR_TOO_MANY_REDIRECTS

结果:ERR_TOO_MANY_REDIRECTS

Test C:

测试 C:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

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

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

Result:ERR_TOO_MANY_REDIRECTS

结果:ERR_TOO_MANY_REDIRECTS

Test D:

测试 D:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

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

Result:ERR_TOO_MANY_REDIRECTS

结果:ERR_TOO_MANY_REDIRECTS

Test E:

测试 E:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

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

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

Result:302 found. Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.

结果:找到 302 个。此外,在尝试使用 ErrorDocument 处理请求时遇到 500 Internal Server Error 错误。

回答by Jason Shah

On Dreamhost, this worked:

在 Dreamhost 上,这有效:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

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

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

回答by Cristiano Maia

Problem solved!

问题解决了!

Final .htaccess:

最终.htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

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

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

回答by henrike

It works for me:

这个对我有用:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !on           
RewriteRule ^(.*) https://%{SERVER_NAME}/ [R,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

回答by Hüseyin Ya?l?

In my case, the htaccess file contained lots of rules installed by plugins like Far Future Expirationand WPSuperCacheand also the lines from wordpress itself.

就我而言,htaccess 文件包含许多由Far Future ExpirationWPSuperCache等插件安装的规则,以及来自 wordpress 本身的行。

In order to not mess things up, I had to put the solution at the top of htaccess (this is important, if you put it at the end it causes some wrong redirects due to conflicts with the cache plugin)

为了不把事情搞砸,我不得不把解决方案放在 htaccess 的顶部(这很重要,如果你把它放在最后,由于与缓存插件冲突,它会导致一些错误的重定向)

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

This way, your lines don't get messed up by wordpress in case some settings change. Also, the <IfModule>section can be repeated without any problems.

这样,如果某些设置发生更改,您的行就不会被 wordpress 弄乱。此外,该<IfModule>部分可以毫无问题地重复。

I have to thank Jason Shahfor the neat htaccess rule.

我要感谢Jason Shah整洁的htaccess 规则

回答by Indigo

For your information, it really depends on your hosting provider.

对于您的信息,这实际上取决于您的托管服务提供商。

In my case (Infomaniak), nothing above actually worked and I got infinite redirect loop.

就我而言(Infomaniak),上面没有任何实际工作,我得到了无限重定向循环。

The right way to do this is actually explained in their support site:

正确的方法实际上在他们的支持站点中有解释

RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://your-domain.com/ [R=301,L]

So, always check with your hosting provider. Hopefully they have an article explaining how to do this. Otherwise, just ask the support.

因此,请务必咨询您的托管服务提供商。希望他们有一篇文章解释如何做到这一点。否则,请寻求支持。

回答by l2aelba

This is tested and safe to use

这是经过测试且可以安全使用的

Why?: When Wordpress editing your re-write rules, so make sure your HTTPS rule should not be removed! so this is no conflictwith native Wordpress rules.

为什么?:当 Wordpress编辑您的重写规则时,请确保您的HTTPS 规则不应被删除!所以这与原生 Wordpress 规则没有冲突

<IfModule mod_rewrite.c>
   RewriteCond %{HTTPS} !=on
   RewriteRule ^(.*) https://%{SERVER_NAME}/ [R,L]
</IfModule>

# BEGIN WordPress
<IfModule mod_rewrite.c>
  #Your our Wordpress rewrite rules...
</IfModule>
# END WordPress

Note:You have to change WordPress Address& Site Addressurls to https://in General Settingsalso (wp-admin/options-general.php)

注意:您还必须在常规设置中将WordPress 地址站点地址url更改为( )https://wp-admin/options-general.php

回答by lundgren

Nothing of the above worked for me. But those lines solved the same problem on my WordPress site:

以上都没有对我有用。但是这些行在我的 WordPress 网站上解决了同样的问题:

RewriteEngine On

RewriteCond %{HTTP:HTTPS} !on
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

回答by Steve

I found all solutions listed on this Q&A did not work for me, unfortunately. What did work was:

不幸的是,我发现此问答中列出的所有解决方案都不适用于我。有效的是:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*)  [L]
RewriteRule ^(.*\.php)$  [L]
RewriteRule . index.php [L]

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ "https\:\/\/www\.example\.com\/" [R=301,L]
</IfModule>
# End Wordpress

Note, the above Wordpress rules are for Wordpress in multi user network mode. If your Wordpress is in single site mode, you would use:

请注意,以上 Wordpress 规则适用于多用户网络模式下的 Wordpress。如果您的 Wordpress 处于单站点模式,您将使用:

# 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]

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ "https\:\/\/www\.example\.com\/" [R=301,L]
</IfModule>
# End Wordpress

回答by Rafael

easyest way to redirect http to https in wordpressit to modify site_url and home from http://example.comto https://example.com. Wordpress will do the redirection. ( that is why you get "too many redirects" error, wordpress is redirecting to http while .htaccess will redirect to https )

wordpress中将 http 重定向到 https 的最简单方法是将site_url 和 home 从http://example.com修改为https://example.com。Wordpress 将进行重定向。(这就是为什么您会收到“重定向过多”错误的原因,wordpress 正在重定向到 http 而 .htaccess 将重定向到 https )

回答by Higher Coding

Here is an alternative solution you can use if you don't want to edit .htaccess:

如果您不想编辑,可以使用以下替代解决方案.htaccess

add_action( 'template_redirect', 'nonhttps_template_redirect', 1 );

function nonhttps_template_redirect() {

    if ( is_ssl() ) {

        if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'https' ) ) {

            wp_redirect( preg_replace( '|^http://|', 'https://', $_SERVER['REQUEST_URI'] ), 301 );

            exit();

        } else {

            wp_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 301 );

            exit();

        }

    }

}

You can place this at the bottom of your theme functions.php

你可以把它放在你主题的底部 functions.php