将非www重定向到www WITHOUT强制https

时间:2018-03-12 11:20:21

标签: .htaccess redirect url-redirection

我的客户有一个http SEO'ed wordpress网站,https AdWords静态html登陆页面(我知道 - 不要问)。 他们希望301所有非www到www。我尝试了以下htaccess代码:

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

这非常适合将所有网址从非www重定向到www。但是,它还会将所有https网址更改为http。

我怎样才能让所有非www重定向到www,但http和https不受影响。

例如:

http://example.com/seo-page - > http://www.example.com/seo-page

https://example.com/ppc-page - > https://www.example.com/ppc-page

由于

EDT: 现有的.htaccess是:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule .* - [E=CANONICAL:http://%{HTTP_HOST}%{REQUEST_URI},NE]
RewriteCond %{HTTPS} =on
RewriteRule .* - [E=CANONICAL:https://%{HTTP_HOST}%{REQUEST_URI},NE]
</IfModule>

2 个答案:

答案 0 :(得分:1)

试试这个:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)
RewriteCond %{HTTPS} !=on
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} =on
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

保存.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} !^(www\.) 
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTPS}:s on:(s)
RewriteRule .* - [E=CANONICAL:http%1://www.%{HTTP_HOST}%{REQUEST_URI},NE]

RewriteCond %{HTTP_HOST} ^(www\.)
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTPS}:s on:(s)
RewriteRule .* - [E=CANONICAL:http%1://%{HTTP_HOST}%{REQUEST_URI},NE]
</IfModule>

答案 1 :(得分:0)

如果您使用的是WordPress,则无需在.htaccess文件中添加任何代码。您可以通过在设置中输入www或nonwww url来重定向整个站点。

登录 wp-admin 然后点击设置按钮并访问 第三个字段名称(WordPress地址(URL))输入您要重定向网站的网址。并在下一个字段名称(网站地址(URL))

中粘贴相同的网址

然后点击保存按钮

祝贺您的网站成功重定向。