使用.htaccess将HTTP重定向到HTTP

时间:2015-08-17 11:59:40

标签: wordpress .htaccess http redirect https

请不要推荐long and very detailed thread超过173个赞成票。它对我没有用。我还尝试了很多其他人(1234)。他们都给了我TOO_MANY_REDIRECTS或错误500.所以,这是我的问题:

使用我当前的.htaccess,就会发生这种情况:

https://www.dukescasino.com/ - 效果很好

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

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

http://www.dukescasino.com/

http://dukescasino.com/

这是当前的.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>

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

  • 高级自定义字段
  • All In One SEO Pack
  • Bop搜索框项目类型导航菜单
  • 联络表格7
  • 禁用评论
  • Google XML Sitemaps
  • Jetpack by WordPress.com
  • 搜索&amp;过滤
  • Slider WD
  • TablePress
  • UpdraftPlus - 备份/恢复
  • Wordfence安全
  • WPide
  • WP Smush
  • WP Super Cache

编辑1 - 已完成测试:

测试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>

结果: ERR_TOO_MANY_REDIRECTS

测试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>

结果: ERR_TOO_MANY_REDIRECTS

测试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>

结果: ERR_TOO_MANY_REDIRECTS

测试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>

结果: ERR_TOO_MANY_REDIRECTS

测试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>

结果: 302找到了。此外,尝试使用ErrorDocument处理请求时遇到500内部服务器错误错误。

17 个答案:

答案 0 :(得分:134)

在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>

答案 1 :(得分:86)

问题解决了!

最终.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>

答案 2 :(得分:14)

它对我有用:

# 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

答案 3 :(得分:11)

就我而言,htaccess文件包含很多插件安装的规则,如远期未来期限 WPSuperCache 以及wordpress本身的行。

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

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

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

这样一来,如果某些设置发生变化,你的线条就不会被wordpress搞砸了。此外, <IfModule> 部分可以毫无问题地重复。

我要感谢Jason Shah整洁htaccess rule

答案 4 :(得分:4)

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

在我的情况下(Infomaniak),上面没有任何实际工作,我得到了无限的重定向循环。

正确的方法是actually explained in their support site

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

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

答案 5 :(得分:2)

经测试且使用安全

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

applicationContext.getBean(Pool.class)

注意:您必须更改 WordPress地址&amp; 网站地址 常规设置中的<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 网址(https://

答案 6 :(得分:1)

上述任何内容都不适合我。但是那些线在我的WordPress网站上解决了同样的问题:

RewriteEngine On

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

答案 7 :(得分:1)

wordpress 中将http重定向到https的最简单方法是将site_url和home从http://example.com修改为https://example.com。 Wordpress将进行重定向。 (这就是为什么你得到&#34;太多的重定向&#34;错误,wordpress重定向到http而.htaccess将重定向到https)

答案 8 :(得分:1)

我发现这个Q&amp; A上列出的所有解决方案都不适合我。工作是什么:

# 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

注意,上述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

答案 9 :(得分:0)

如果您不想编辑.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();

        }

    }

}

您可以将其放在主题functions.php

的底部

答案 10 :(得分:0)

在WordPress中添加这个&#39; .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]

因此默认的WordPress&#39; .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>

答案 11 :(得分:0)

以上最终的.htaccess和测试A,B,C,D,E对我不起作用。我只使用了以下2行代码,它可以在我的WordPress网站上运行:

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

我不确定我在哪里弄错了,但这页帮助了我。

答案 12 :(得分:0)

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

答案 13 :(得分:0)

没有,如果这对我有用。首先,我必须查看我的提供商,了解他们如何在我的提供商提供的.htaccess中激活SSL

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

但是我花了几天研究的时间是因为我提供的网站位于代理服务器后面而必须添加wp-config.php以下行:

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

答案 14 :(得分:0)

只需在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

答案 15 :(得分:0)

从http重定向到https://www

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

这肯定可以工作!

答案 16 :(得分:-1)

只需添加此代码,它就像魅力一样:

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