如何重定向所有服务器别名,然后www www in .htaccess in .htaccess in .htaccess

发布于 2025-02-01 03:06:50 字数 1901 浏览 3 评论 0原文

寻找一种将使用Apache VirtualHosts和.htaccess的方法重定向多个服务器别名(www and nonwww)将多个服务器别名(www and nonwww)重定向到一个非www域。我现在已经搜索了一段时间,已经找到了几种解决方案,但是它们都在某种程度上起作用。

情况如下,在我的.conf文件中,我有一个虚拟主机指定为:

<VirtualHost *:443>

        ServerName example.domain
        ServerAlias *.example.domain *.exampledomain.com exampledomain.com

        ...

</VirtualHost>
<VirtualHost *:80>

        ServerName example.domain
        ServerAlias *. example.domain *.exampledomain.com exampledomain.com

        RewriteEngine on

        RewriteCond %{SERVER_NAME} =www.example.domain [OR]
        RewriteCond %{SERVER_NAME} =example.domain
        RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

</VirtualHost>

接下来,我在.htaccess:结果中有以下内容:

        RewriteEngine On
        Options +FollowSymlinks

        RewriteBase /

        RewriteCond %{HTTPS} off
        RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

        RewriteCond %{HTTP_HOST} ^www.exampledomain.com$ [OR]
        RewriteCond %{HTTP_HOST} ^exampledomain.com$ [OR]
        RewriteCond %{HTTP_HOST} ^www\.example\.domain$
        RewriteRule ^(.*)$ https://example.domain/$1 [L,R=301]

结果如下:

http://example.domain/          -> https://example.domain/ - (correct)
http://www.example.domain/      -> https://example.domain/ - (correct)

http://exampledomain.com/       -> http://exampledomain.com/ - Forbidden, you dont have access...
http://www.exampledomain.com/   -> http://www.exampledomain.com/ - Forbidden, you dont have access...

https://exampledomain.com/      -> https://example.domain/ - (correct)
https://www.exampledomain.com/  -> https://www.exampledomain.com/ - Connection not secure

我真的无法弄清楚这出错了哪里,为什么有些重定向工作而其他重定向不行。任何提示都将不胜感激。

Looking for a way to redirect several server aliases (both www and non-www) to one non-www domain with forced SSL using Apache VirtualHosts and .htaccess. I've been searching for a while now and have found several solutions but they all work partially.

The situation is as follows, in my .conf file I have a virtual host specified as follows:

<VirtualHost *:443>

        ServerName example.domain
        ServerAlias *.example.domain *.exampledomain.com exampledomain.com

        ...

</VirtualHost>
<VirtualHost *:80>

        ServerName example.domain
        ServerAlias *. example.domain *.exampledomain.com exampledomain.com

        RewriteEngine on

        RewriteCond %{SERVER_NAME} =www.example.domain [OR]
        RewriteCond %{SERVER_NAME} =example.domain
        RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

</VirtualHost>

Next, I have the following in my .htaccess:

        RewriteEngine On
        Options +FollowSymlinks

        RewriteBase /

        RewriteCond %{HTTPS} off
        RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

        RewriteCond %{HTTP_HOST} ^www.exampledomain.com$ [OR]
        RewriteCond %{HTTP_HOST} ^exampledomain.com$ [OR]
        RewriteCond %{HTTP_HOST} ^www\.example\.domain$
        RewriteRule ^(.*)$ https://example.domain/$1 [L,R=301]

Result is as follows:

http://example.domain/          -> https://example.domain/ - (correct)
http://www.example.domain/      -> https://example.domain/ - (correct)

http://exampledomain.com/       -> http://exampledomain.com/ - Forbidden, you dont have access...
http://www.exampledomain.com/   -> http://www.exampledomain.com/ - Forbidden, you dont have access...

https://exampledomain.com/      -> https://example.domain/ - (correct)
https://www.exampledomain.com/  -> https://www.exampledomain.com/ - Connection not secure

I really can't figure out where this goes wrong and why some redirects work and others don't. Any hints would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼波传意 2025-02-08 03:06:50

如果您可以访问&lt; virtualHost&gt;,那么您就不需要(不应该)使用.htaccess

如果目标是将单个重定向的规范域(+https)重定向,则仅使用定义的两个VHOST,而您所需要的只是:

<VirtualHost *:443>
    ServerName example.domain
    ServerAlias *.example.domain *.exampledomain.com exampledomain.com

    RewriteEngine On

    # Redirect everything other than the canonical host to the canonical host
    RewriteCond %{HTTP_HOST} !=example\.domain
    RewriteRule ^ https://example.domain%{REQUEST_URI} [R=301,L]
</VirtualHost>

<VirtualHost *:80>
    ServerName example.domain
    ServerAlias *.example.domain *.exampledomain.com exampledomain.com

    # Unconditionally redirect everything to HTTPS + canonical host
    Redirect 301 / https://example.domain/
</VirtualHost>

mod_alias redirect指令是前缀匹配和比赛后的所有内容附加到目标URL的末尾。因此,重定向上述指令将每个URL重定向到目标的同一URL。

您应首先使用302(临时)重定向测试,并且仅一旦确认其按预期工作就可以更改为301(永久)。您可能需要清除浏览器缓存,因为浏览器将301持续缓存。


查看您的“结果”:

http://exampledomain.com/       -> http://exampledomain.com/ - Forbidden, you dont have access...
http://www.exampledomain.com/   -> http://www.exampledomain.com/ - Forbidden, you dont have access...

当前的HTTP到https重定向:80容器仅重定向www.example.example.domain example.domain.domain < /code>,并且您可能不接受VHOST中的请求:80个容器,因此任何HTTP请求(未重定向到HTTPS)可能都会阻止。

https://www.exampledomain.com/  -> https://www.exampledomain.com/ - Connection not secure

您的SSL证书需要涵盖所有域和所有别名,否则,您(充其量)将获得浏览器SSL证书警告,并且浏览器将拒绝连接到服务器(因此看不到重定向)。

If you have access to the <VirtualHost> then you don't need to (should not) use .htaccess at all for this.

If the goal is to redirect to the canonical domain (+HTTPS) in a single redirect using just the two vHosts as defined then all you need is:

<VirtualHost *:443>
    ServerName example.domain
    ServerAlias *.example.domain *.exampledomain.com exampledomain.com

    RewriteEngine On

    # Redirect everything other than the canonical host to the canonical host
    RewriteCond %{HTTP_HOST} !=example\.domain
    RewriteRule ^ https://example.domain%{REQUEST_URI} [R=301,L]
</VirtualHost>

<VirtualHost *:80>
    ServerName example.domain
    ServerAlias *.example.domain *.exampledomain.com exampledomain.com

    # Unconditionally redirect everything to HTTPS + canonical host
    Redirect 301 / https://example.domain/
</VirtualHost>

The mod_alias Redirect directive is prefix matching and everything after the match is appended to the end of the target URL. So, the Redirect directive above redirects every URL to the same URL at the target.

You should test first with 302 (temporary) redirects and only change to a 301 (permanent) once you have confirmed it works as intended. You will likely need to clear your browser cache since 301s are cached persistently by the browser.


A look at your "results":

http://exampledomain.com/       -> http://exampledomain.com/ - Forbidden, you dont have access...
http://www.exampledomain.com/   -> http://www.exampledomain.com/ - Forbidden, you dont have access...

The current HTTP to HTTPS redirect in the vHost:80 container is only redirecting www.example.domain and example.domain and you probably aren't accepting requests in the vHost:80 container, so any HTTP request (that is not redirected to HTTPS) is probably blocked.

https://www.exampledomain.com/  -> https://www.exampledomain.com/ - Connection not secure

Your SSL cert needs to cover all domains and all aliases, otherwise, you will (at best) get a browser SSL cert warning and the browser will refuse to connect to your server (so does not see the redirect).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文