将所有可能的域和子域永久重定向到https,而不使用www(https://example.com)

时间:2018-07-18 03:55:03

标签: apache performance .htaccess mod-rewrite https

我的网站访问量很大,因此性能至关重要。我正在使用apache和ubuntu。

我正在尝试将所有可能的域可能性都重定向到https://example.com,包括所有子域(https://test.example.com)。

这是我目前拥有的:

RewriteEngine On

# match any URL with www and rewrite it to https without the www
RewriteCond %{HTTP_HOST} ^(www\.)(.*) [NC]
RewriteRule (.*) https://%2%{REQUEST_URI} [L,R=301]

# match urls that are non https (without the www)
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^(www\.)(.*) [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

我觉得上面的代码在做额外的路由/工作。在虚拟主机文件或.htaccess中是否有更直接的解决方案(以提高性能)?

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以结合所有三个条件

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

说明:如果not https OR not example.com,然后重定向到https://example.com/xxx

要回答有关性能的问题:用.htaccess重写可能比用Apache配置文件重写慢。

相关问题