删除www以及将https添加到域名

时间:2014-08-12 08:24:11

标签: apache .htaccess mod-rewrite rewrite

我希望进行以下重写:

http://website.com        >  https://website.com
www.website.com           >  https://website.com
https://www.website.com   >  https://website.com

基本上删除每个组合的www并将所有内容重定向到https。

下面是当前的.htaccess配置,似乎解决了种类上面的第一个和第二个,但在第三个上分崩离析。无法看到这里缺少的东西。

<IfModule mod_rewrite.c>

RewriteEngine On

# remove www if it has it
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ http://website.com/$1 [L,R,QSA]

# send everything to https
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# BEGIN WordPress
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

</IfModule>

1 个答案:

答案 0 :(得分:1)

实际上,这可以在单一规则中完成:

RewriteEngine On
RewriteBase /

# remove www if it has it
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteRule ^ https://website.com%{REQUEST_URI} [L,R=302,NE]

# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress