简体   繁体   English

Apache2 RedirectMatch 在特定情况下不起作用

[英]Apache2 RedirectMatch not working in specific case

I have a server using apache2 that redirects all HTTP requests to HTTPS:我有一个使用 apache2 的服务器,它将所有 HTTP 请求重定向到 HTTPS:

<VirtualHost *:80>
        ServerName www.mydomain.com.au
        RedirectMatch permanent ^/?(.*) https://www.mydomain.com.au/$1
</VirtualHost>

<VirtualHost *:80>
        ServerName mydomain.com.au
        RedirectMatch permanent ^/?(.*) https://www.mydomain.com.au/$1
</VirtualHost>

It works perfectly with the www domain but whenever I try to access the 'bare' domain I get the error:它与 www 域完美配合,但每当我尝试访问“裸”域时,我都会收到错误消息:

Not Found
The requested URL / was not found on this server.

The most baffling thing is that the HTTPS version works:最莫名其妙的是HTTPS版本的工作原理:

<VirtualHost *:443>
        ServerName mydomain.com.au
        RedirectMatch permanent ^/?(.*) https://www.mydomain.com.au/$1
        #...
</VirtualHost>

If I for example try to GET https://mydomain.com.au/contact , it will correctly redirect to https://www.mydomain.com.au/contact .例如,如果我尝试 GET https://mydomain.com.au/contact ,它将正确重定向到https://www.mydomain.com.au/contact However, it will fail if I do the same thing with HTTP.但是,如果我对 HTTP 执行相同的操作,它将失败。 Why?为什么?

You shouldn't have 2 entries for VirtualHost *:80 You can use ServerAlias for www and have a single entry: VirtualHost *:80不应有 2 个条目,您可以将ServerAlias用于www并只有一个条目:

<VirtualHost *:80>
        ServerName mydomain.com.au
        ServerAlias www.mydomain.com.au
        RedirectMatch permanent ^/?(.*) https://www.mydomain.com.au/$1
</VirtualHost>

ServerAlias declares alternative names that can be used to address the same site as in ServerName . ServerAlias声明可用于寻址与ServerName相同的站点的备用名称。

It turns out that the problem was that my VirtualHost settings were being overwritten in sites-available/000-default.conf.事实证明,问题在于我的 VirtualHost 设置在 sites-available/000-default.conf 中被覆盖。 I changed the therein to have the configuration suggested by anubhava and it's working fine.我更改了其中的配置以使用 anubhava 建议的配置,并且工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM