将所有.org .ca .net域重定向到主域

时间:2019-11-01 19:23:01

标签: apache redirect apache2

我们的服务器设置是ubuntu上的apache2。

我们已经购买了example.com,example.ca,example.org和example.net,我试图将这些示例的所有版本重定向到我们的https://www.example.com,同时尝试尽可能地保持干​​燥。我发现了许多将http重定向到https或www的示例。到非www。但是在一般情况下我找不到太多要发送的全部规则:

http://www.example.ca
http://example.ca
https://example.ca
https://www.example.ca

(与上述.org,.net和.com相同)

全部为https://www.example.com

我已将以下规则添加到我们的000-default.conf文件中。我想知道是否有一种更干净的方法来编写这些规则以更好地重定向到https://www.example.com,并且我还在寻求将所有示例的所有https请求重定向到https://www.example.com的帮助。请让我知道我是否要去吃午餐。

非常感谢您的帮助。

000-default.conf

# Redirect .net to .com
<VirtualHost *:80>
    ServerName www.example.net
  ServerAlias example.net
  RewriteEngine on
    RewriteCond %{HTTP_HOST} ^(www\.)?example\.(net)$ [NC]
    RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,R=301]
</VirtualHost>

# Redirect .org to .com
<VirtualHost *:80>
    ServerName www.example.org
  ServerAlias example.org
  RewriteEngine on
    RewriteCond %{HTTP_HOST} ^(www\.)?example\.(org)$ [NC]
    RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,R=301]
</VirtualHost>

# Redirect .ca to .com
<VirtualHost *:80>
    ServerName www.example.ca
  ServerAlias example.ca
  RewriteEngine on
    RewriteCond %{HTTP_HOST} ^(www\.)?example\.(ca)$ [NC]
    RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,R=301]
</VirtualHost>

# Redirect back to example.com
<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com
    RewriteEngine on
  RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]
</VirtualHost>

# Enable ssl certifications
<VirtualHost *:443>
    DocumentRoot /var/www/html
    ServerName www.example.com
    ServerAlias example.com
    SSLEngine on
    SSLCertificateFile path to chain
    SSLCertificateKeyFile path to key
</VirtualHost>

1 个答案:

答案 0 :(得分:1)

# Redirect anything http:// to https://www.example.com
<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com example.net www.example.net example.org www.example.org example.ca www.example.ca
    RewriteEngine on
    RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=302]
</VirtualHost>

<VirtualHost *:443>
    ServerName www.example.com
    ServerAlias example.com example.net www.example.net example.org www.example.org example.ca www.example.ca
    SSLEngine on
    SSLCertificateFile path to chain
    SSLCertificateKeyFile path to key
    RewriteEngine on
    # Redirect anything not www.example.com to https://www.example.com
    RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
    RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=302]
</VirtualHost>

确认其可以正常工作后,将302替换为301