将.htaccess重定向到ssl不起作用

时间:2014-10-07 07:09:36

标签: php wordpress .htaccess redirect ssl

我试图将xyz.com重定向到https://xyz.com

我的代码

# BEGIN WordPress
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^xyz.com$
RewriteRule ^(.*)$ https://xyz.com/$1 [R=301]

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]
RewriteRule ^(.*/)?sitemap.xml wp-content/sitemap.php [L]

# END WordPress

我也尝试以下代码

RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

它显示此错误 该页面未正确重定向

实际上它是一个 WordPress 网站。

如何解决这个问题?

提前致谢

4 个答案:

答案 0 :(得分:2)

请遵守此规则:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://xyz.com%{REQUEST_URI} [R=301,L]

另外,请确保在WP永久链接设置中,您有homesite网址反映https://xyz.com/

答案 1 :(得分:1)

请将以下代码添加到.htaccess文件中,以将整个网站重定向到https。

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*) https://%{SERVER_NAME}/$1 [R,L]

如果要重定向所选页面,请按以下方式重写规则。

RewriteRule ^page1(.*) https://%{SERVER_NAME}/page1$1 [R,L]

如果您更喜欢插件,可以使用此插件:https://wordpress.org/plugins/https-redirection/

答案 2 :(得分:1)

试试这个为我工作

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

答案 3 :(得分:1)

请将此代码添加到.htaccess文件中并告诉我。

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

此代码在我的网站中运行正常,请检查您的托管服务提供商是否拥有SSL证书。

点击此链接http://kuldipmakdiya.wordpress.com/2014/10/14/redirect-htaccess-to-ssl-is-not-working/