【问题标题】:htaccess wordpress 301 redirecthtaccess wordpress 301重定向
【发布时间】:2013-02-28 22:05:53
【问题描述】:

我最近将 SSL 添加到我的 wordpress 站点,现在如果有一些访问我的 http 站点,我想将它们重定向到我的 https 站点。我尝试了以下方法:

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

但是出现了这个错误

Error 310 (net::ERR_TOO_MANY_REDIRECTS): There were too many redirects.

我做错了什么?

【问题讨论】:

    标签: wordpress .htaccess redirect ssl


    【解决方案1】:

    试试这个:

    RewriteEngine on
    
    RewriteCond %{SERVER_PORT} 80
    
    RewriteRule ^(.*)$ https://{SERVER_NAME}/$1 [R=301,L]
    
    RewriteCond %{HTTP_HOST} ^www\.(.*)
    
    RewriteRule ^(.*)$ https://{SERVER_NAME}/$1 [R=301,L]
    
    RewriteCond %{HTTP_HOST} ^http://\.(.*)
    
    RewriteRule ^(.*)$ https://{SERVER_NAME}/$1 [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} -s [OR]
    
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    
    RewriteCond %{REQUEST_FILENAME} -d
    
    RewriteRule ^.*$ - [NC,L]
    
    RewriteRule ^.*$ /index.php [NC,L]
    

    【讨论】:

      【解决方案2】:

      这是您应该用于强制执行 https 的代码:

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

      【讨论】: