htaccess将多个域,https和http,www和非www重定向到一个https域

时间:2017-06-01 18:42:32

标签: apache .htaccess redirect mod-rewrite

我有三个不同的域名,我想确保所有域名都重定向到一个域名。

我想确保涵盖所有案例:如果用户键入http或https,如果它们包含www或不包含www。

这创造了12种不同的可能性......

usort

我需要将所有这些重定向到:

$users = [
    0 => [
        'user_id' => 1,
        'user_date' => '2017-04-25',
        'user_name' => 'test',
    ],
    1 => [
        'user_id' => 2,
        'user_date' => '2017-04-26',
        'user_name' => 'test 2',
    ],
    2 => [
        'user_id' => 3,
        'user_date' => '2017-04-28',
        'user_name' => 'test 3',
    ],
    3 => [
        'user_id' => 4,
        'user_date' => '2017-04-28',
        'user_name' => 'test 4',
    ],
    4 => [
        'user_id' => 5,
        'user_date' => '2017-04-26',
        'user_name' => 'test 5',
    ],
];

usort($users, function($user1, $user2){
    // This function sort users by ascending order of date. Compares date. if user 1 has later date than user 2, place him on the bottom of the array
    return strtotime($user1['user_date']) > strtotime($user2['user_date']);
});
var_dump($users);

我已经按照我认为适用于大多数版本的方式设置了我的.htaccess文件,我已经在Google和SO上研究了这个,这对我来说是最好的解决方案:

http://www.domain1.co.uk
https://www.domain1.co.uk
http://domain1.co.uk
https://domain1.co.uk

http://www.domain2.uk
https://www.domain2.uk
http://domain2.uk
https://domain2.uk

http://www.domain3.co.uk
https://www.domain3.co.uk
http://domain3.co.uk
https://domain3.co.uk

但这仅涵盖了其中的11种。

使用此htaccess文件,如果我访问URL:

https://www.domain3.co.uk

它没有重定向。

根据需要重定向所有其他变体。

如何更改我的htaccess文件,以便涵盖所有12种可能的变体?

1 个答案:

答案 0 :(得分:0)

您可以将此单一规则与[OR]子句一起使用:

RewriteEngine On

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.domain3\.co.uk$ [NC]
RewriteRule ^ https://www.domain3.co.uk%{REQUEST_URI} [R=301,L,NE]
相关问题