简体   繁体   English

如何将http://domain.com重定向到https://www.domain.com?

[英]How to redirect http://domain.com to https://www.domain.com?

I already got an SSL certificate. 我已经获得了SSL证书。 I got Wordpress installed see folder structure in my hostgator file manager below: 我安装了Wordpress,请参阅我的hostgator文件管理器中的文件夹结构:

I already put define('FORCE_SSL_ADMIN', true); 我已经把define('FORCE_SSL_ADMIN', true); in wp_config.php wp_config.php

Wordpress General settings: WordPress Address (URL): https://www.domain.com/blog Site Address (URL): https://www.domain.com Wordpress常规设置:WordPress地址(URL): https//www.domain.com/blog网站地址(URL): https//www.domain.com

public_html
  -> blog (inside is the wordpress installation)

database structure
  -> wp_options
      -> siteurl: https://www.domain.com/blog
      -> home: https://www.domain.com

I tried hostgator cpanel redirects, but not works. 我尝试了hostgator cpanel重定向,但没有用。

I also tried some of the solutions editing .htaccess while searching in google. 在谷歌搜索时,我也尝试了一些编辑.htaccess的解决方案。

Maybe I missed on something regarding with the folder structure. 也许我错过了关于文件夹结构的一些事情。

.htaccess : .htaccess

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On

RewriteBase /blog/

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

# END WordPress

You typically just add the rules to your wordpress rules. 您通常只需将规则添加到wordpress规则中。 They go before wp rules. 他们在wp规则之前走了。

RewriteEngine On
#rewrite http to https and add www. All cases covered
RewriteCond %{HTTPS} !^on [OR]
RewriteRule %{HTTP_HOST} ^exmaple\.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

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

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

# END WordPress

You will need 2 virtual hosts, one for port 443 (https) and one for port 80. In the port 80 you could simply setup a redirect rule for all traffic 您将需要2个虚拟主机,一个用于端口443(https),另一个用于端口80.在端口80中,您只需为所有流量设置重定向规则

  <VirtualHost *:80>
  ...
    RewriteEngine On
    RewriteRule .* https://www.domain.com [R=301,L]
  ...
  </VirtualHost>

Use 使用

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

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

to force SSL on all conditions (with or without the www ). 在所有条件下强制使用SSL(使用或不使用www )。

Also, as you have done, change to https in Wordpress General settings. 此外,正如您所做的那样,在Wordpress常规设置中更改为https

Do not add the force SSL rule inside the # BEGIN WordPress and # END WordPress rewrite block, becuase if you ever resave permalinks (or even a plugin does) your SSL rules will be deleted. 不要# BEGIN WordPress# END WordPress重写块中添加强制SSL规则,因为如果您重新保存永久链接(甚至是插件),您的SSL规则将被删除。

And then use the developer tools in Firefox (or Firebug ) or Chrome or Safari or IE to see if you have any non-secure element errors, such as from hardcoded http:// URLs in stylesheets and functions.php files. 然后使用Firefox (或Firebug )或ChromeSafariIE中的开发人员工具查看是否存在任何non-secure元素错误,例如样式表和functions.php文件中的硬编码http:// URL。

This is my last resorts for the fix. 这是我修复的最后一个度假胜地。 I put up a php code in the header.php that checks if the inputted url is https if not then it redirects to the url with https. 我在header.php中设置了一个php代码,用于检查输入的url是否为https,如果没有,则重定向到https的url。

<?php
    if (empty($_SERVER['HTTPS'])) {
        header('Location: https://domain.com');
        exit;
    }
?>

Thanks all, 谢谢大家,

暂无
暂无

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

相关问题 SSL错误:如何通过.htaccess将https://www.domain.com重定向到http://domain.com以避免SSL错误 - SSL Error: how to redirect https://www.domain.com to http://domain.com via .htaccess to avoid SSL error 将 domain.com/shop 重定向到 www.domain.com/shop - Redirect domain.com/shop to www.domain.com/shop http to https AND //domain.com to www.domain.com AND subdomain.domain.com to subdomain.domain.com through htaccess - http to https AND //domain.com to www.domain.com AND subdomain.domain.com to subdomain.domain.com through htaccess htaccess将domain.com重定向到www.domain.com,但多个域的staging.domain.com除外 - htaccess redirect domain.com to www.domain.com except of staging.domain.com for multiple domains 继续从www.domain.com重定向到domain.com - keep redirecting from www.domain.com to domain.com 可从domain.com和www.domain.com访问Cookie - Cookies accessible from domain.com and www.domain.com 从http://domain.com切换到http://www.domain.com时,PHP会话丢失 - PHP Session lost when switching from http://domain.com to http://www.domain.com 如何在wordpress中同时启用domain.com和www.domain.com - how to enable both domain.com and www.domain.com in wordpress 2个不同的php会话:www.domain.com和domain.com:如何整合? - 2 different php sessions: www.domain.com and domain.com: How to consolidate? Yii ::将www.domain.com/index.php重定向到www.domain.com,而不使用htaccess - Yii:: redirect www.domain.com/index.php to www.domain.com without using htaccess
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM