修改.htaccess SSL重定向以处理www且没有子域的最佳方法

时间:2018-07-02 15:24:12

标签: .htaccess

我是否可以通过某种巧妙的正则表达式来修改此工作.htaccess,以处理多个子域的重定向。

理想情况下,我希望能够使用我的joshmoto.wtf域而无需在前面放置www

是否可以使当前的.htaccess功能如下所示?

如果您访问...

  1. http://www.joshmoto.wtf,它将重定向到https://www.joshmoto.wtf
  2. http://joshmoto.wtf,它将重定向到https://joshmoto.wtf

请参见下面的当前.htaccess,它始终将http://joshmoto.wtf重定向到https://www.joshmoto.wtf

<IfModule mod_rewrite.c>
    # Force SSL access
    RewriteEngine On
    RewriteBase /

    RewriteCond %{HTTP_HOST} !^www\.joshmoto\.wtf [NC]
    RewriteRule ^/?(.*) https://www.joshmoto.wtf/$1 [L,R,NE]
</IfModule>

任何专家正则表达式的帮助将不胜感激。

非常感谢

1 个答案:

答案 0 :(得分:1)

像这样尝试:

RewriteEngine On
RewriteBase /
# Redirect HTTP with www to HTTPS without www
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.joshmoto\.wtf$ [NC]
RewriteRule .* https://joshmoto.wtf%{REQUEST_URI} [R=301,L]
# Redirect HTTP without www to HTTPS without www
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Redirect HTTPS with www to HTTPS without www
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.joshmoto\.wtf$ [NC]
RewriteRule .* https://joshmoto.wtf%{REQUEST_URI} [R=301,L]