移动到 https://www,当它是 www 或 http:// 或没有 www 和 http:// 时?

Move to https://www, when it is www or http:// or without www and http://?

我正在使用 Zend 框架,其中有漂亮的 url 控制器。以下 .htaccess 正在运行,但它使 SEO 将我们视为一页的四个链接。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]

我需要使用 htaccess 进行以下修复:

www.whosebug.com/nice-looking-url = https://www.whosebug.com/nice-looking-url

whosebug.com/nice-looking-url = https://www.whosebug.com/nice-looking-url

http://www.whosebug.com/nice-looking-url = https://www.whosebug.com/nice-looking-url

http://whosebug.com/nice-looking-url = https://www.whosebug.com/nice-looking-url

如何使用 htaccess 正确地做到这一点?这样上面的输入 url 就可以安全地到达 https://www。总是在前面?

像这样拥有完整的 .htaccess:

RewriteEngine On

## add www and turn on https in same rule - main domain
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.com)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

## turn on https in same rule - sub domain
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]

确保使用新浏览器测试您的更改以避免旧浏览器缓存。