简体   繁体   English

使用.htaccess将HTTP重定向到HTTP

[英]Use .htaccess to redirect HTTP to HTTPs

Please, don't recommend me the long and very detailed thread with more than 173 upvotes. 请不要向我推荐超过173个upvotes的长而非常详细的帖子。 It didn't work for me. 它对我不起作用。 I have also tried many others ( 1 , 2 , 3 , 4 ). 我还尝试了许多其他( 1234 )。 They all give me TOO_MANY_REDIRECTS or error 500. So, here is my issue: 他们都给我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 All In One SEO Pack
  • Bop Search Box Item Type For Nav Menus 波普搜索框项目类型为导航菜单
  • Contact Form 7 联系表格7
  • Disable Comments 禁用评论
  • Google XML Sitemaps Google XML Sitemaps
  • Jetpack by WordPress.com Jetpack by WordPress.com
  • Search & Filter 搜索和过滤
  • Slider WD 滑块WD
  • TablePress TablePress
  • UpdraftPlus - Backup/Restore UpdraftPlus - 备份/恢复
  • Wordfence Security Wordfence安全
  • WPide WPide
  • WP Smush WP Smush
  • WP Super Cache 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: 测试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}$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}$1

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

Result: 302 found. 结果: 302找到了。 Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request. 此外,尝试使用ErrorDocument处理请求时遇到500内部服务器错误错误。

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>

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>

It works for me: 这个对我有用:

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

# END WordPress

In my case, the htaccess file contained lots of rules installed by plugins like Far Future Expiration and WPSuperCache and 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. 这样,如果某些设置发生变化,您的线条不会被wordpress弄乱。 Also, the <IfModule> section can be repeated without any problems. 此外, <IfModule>部分可以重复,没有任何问题。

I have to thank Jason Shah for the neat htaccess rule . 我要感谢Jason Shah的整洁htaccess规则

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/$1 [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. 否则,只需询问支持。

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 ! 为什么?:当Wordpress 编辑您的重写规则时 ,请确保不要删除您的HTTPS规则 so this is no conflict with native Wordpress rules. 所以这与本机Wordpress规则没有冲突

<IfModule mod_rewrite.c>
   RewriteCond %{HTTPS} !=on
   RewriteRule ^(.*) https://%{SERVER_NAME}/$1 [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 Address urls to https:// in General Settings also ( wp-admin/options-general.php ) 注意:您还必须在常规设置中将 WordPress地址站点地址 URL更改为https://wp-admin/options-general.php

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).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [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. 注意,上述Wordpress规则适用于多用户网络模式下的Wordpress。 If your Wordpress is in single site mode, you would use: 如果您的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

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]

easyest way to redirect http to https in wordpress it to modify site_url and home from http://example.com to https://example.com . 最简单的方法是在Wordpress中将http重定向到https,以便将site_url和home从http://example.com修改为https://example.com Wordpress will do the redirection. Wordpress将进行重定向。 ( that is why you get "too many redirects" error, wordpress is redirecting to http while .htaccess will redirect to https ) (这就是为什么你得到“太多的重定向”错误,wordpress重定向到http而.htaccess将重定向到https)

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的底部

Add this in the WordPress' .htaccess file: 在WordPress的.htaccess文件中添加:

RewriteCond %{HTTP_HOST} ^yoursite.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.yoursite.com [NC]
RewriteRule ^(.*)$ https://www.yoursite.com/$1 [L,R=301,NC]

Therefore the default WordPress' .htaccess file should look like this: 因此,默认的WordPress'.htaccess文件应如下所示:

<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} ^yoursite.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.yoursite.com [NC]
RewriteRule ^(.*)$ https://www.yoursite.com/$1 [L,R=301,NC]
</IfModule>

The above final .htaccess and Test A,B,C,D,E did not work for me. 上面的最终.htaccess和测试A,B,C,D,E对我不起作用。 I just used below 2 lines code and it works in my WordPress website: 我只使用了以下2行代码,它可以在我的WordPress网站上运行:

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

I'm not sure where I was making the mistake but this page helped me out. 我不知道我在哪里弄错了,但这页帮助了我。

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^digitalsoftwaremarket.com [NC]
    RewriteRule ^(.*)$ http://www.digitalsoftwaremarket.com/$1 [L,R=301]
</IfModule>

None if this worked for me. 没有,如果这对我有用。 First of all I had to look at my provider to see how they activate SSL in .htaccess my provider gives 首先,我必须查看我的提供商,了解他们如何在我的提供商提供的.htaccess激活SSL

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteCond %{HTTP:HTTPS} !on
   RewriteRule (.*) https://%{SERVER_NAME}/$1 [QSA,L,R=301]
</IfModule>

But what took me days of research is I had to add to wp-config.php the following lines as my provided site is behind a proxy : 但是我花了几天时间研究的是我必须在wp-config.php添加以下行,因为我提供的站点位于代理后面:

/**
 * Force le SSL
 */
define('FORCE_SSL_ADMIN', true);
if (strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false) $_SERVER['HTTPS']='on';

Just add or replace this code in your .htaccess file in wordpress 只需在wordpress中的.htaccess文件中添加或替换此代码即可

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

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

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

# END WordPress

Redirect from http to https://www 从http重定向到https:// www

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

This will work for sure! 这肯定会起作用!

Just add this code, And it will work like charm: 只需添加此代码,它就像魅力一样:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP_HOST}#%{HTTPS}s ^www\.([^#]+)#(?:off|on(s)) [NC]
    RewriteRule ^ http%2://%1%{REQUEST_URI} [R=301,L]
</IfModule>

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

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