Apache 在域和子域上保留https://但删除www

Apache 在域和子域上保留https://但删除www,apache,.htaccess,mod-rewrite,Apache,.htaccess,Mod Rewrite,我一直试图找到一种方法,在我的域和子域上保留https://,同时从URL中删除www. 示例: URL 1: http://www.mywebsite.co.uk Redirect 1: https://mywebsite.co.uk URL 2: http://test.mywebsite.co.uk Redirect 2: https://test.mywebsite.co.uk URL 3: http://mywebsite.co.uk Redirect 3: https://myw

我一直试图找到一种方法,在我的域和子域上保留
https://
,同时从URL中删除
www.

示例:

URL 1: http://www.mywebsite.co.uk
Redirect 1: https://mywebsite.co.uk

URL 2: http://test.mywebsite.co.uk
Redirect 2: https://test.mywebsite.co.uk

URL 3: http://mywebsite.co.uk
Redirect 3: https://mywebsite.co.uk

URL 4: http://www.test.mywebsite.co.uk
Redirect 4: https://test.mywebsite.co.uk
RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{HTTP_HOST} ^(.+\.)?mywebsite\.co\.uk$
RewriteRule ^(.*)$ https://%1mywebsite.co.uk/$1 [R,L]
我遇到过多次尝试解决堆栈溢出问题的尝试,并收到了Apache和PHP的麻烦,需要在我的站点中编写脚本进行修复,但它们似乎总是伴随着其他问题

当前.htaccess文件:

URL 1: http://www.mywebsite.co.uk
Redirect 1: https://mywebsite.co.uk

URL 2: http://test.mywebsite.co.uk
Redirect 2: https://test.mywebsite.co.uk

URL 3: http://mywebsite.co.uk
Redirect 3: https://mywebsite.co.uk

URL 4: http://www.test.mywebsite.co.uk
Redirect 4: https://test.mywebsite.co.uk
RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{HTTP_HOST} ^(.+\.)?mywebsite\.co\.uk$
RewriteRule ^(.*)$ https://%1mywebsite.co.uk/$1 [R,L]

虽然这有助于在我的子域上保留SSL,但我仍然可以通过在URL上预先添加
www.
来访问我的主域和子域。这不是我想要的效果。

您可以使用以下规则:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]

请记住在测试此更改之前清除浏览器缓存。

这仍然允许在子域上使用www.on,而且因为我现在已经有了上面的重写规则,它现在重定向到www.mywebsite.com/dashboard/,使用AngularJS使我的单页应用程序崩溃。这必须作为第一条规则放在
RewriteEngine
行下面,并且您必须清除浏览器缓存。还要确保子域上没有其他规则/代码强制
www