help.ubuntu.com 上的 apache RedirectMatch 并非在所有情况下都重定向

help.ubuntu.com 上的 apache RedirectMatch 并非在所有情况下都重定向

对于 20.04,Ubuntu 服务器指南已移至新位置。重定向已添加到 .htaccess 文件中。但是,它们并不适用于所有 URL。

请注意,help.ubuntu.com 的相关部分每天仅从主启动板分支发布一次。目标是做好这件​​事,因为我昨天显然没有做好。

工作示例:

https://help.ubuntu.com/20.04/serverguide/remote-administration.html.fr

前往:

https://ubuntu.com/server/docs

不起作用的示例:

https://help.ubuntu.com/20.04/serverguide/remote-administration.html

返回 404 未找到。

.htaccess 文件的相关部分:

# For 20.04, and likely onwards, the serveguide has moved.
# Don't try to be too clever, just force the base page and drop the rest.
#
RedirectMatch permanent ^/(stable|lts|20\.04)/(serverguide/.+\.html)\..* https://ubuntu.com/server/docs
RedirectMatch permanent ^/(stable|lts|20\.04)/(serverguide/.+\.pdf)\..* https://assets.ubuntu.com/ubuntu-server-guide

似乎需要语言扩展才能工作。这个能用吗?

RedirectMatch permanent ^/(stable|lts|20\.04)/(serverguide/.+\.html) https://ubuntu.com/server/docs
RedirectMatch permanent ^/(stable|lts|20\.04)/(serverguide/.+\.pdf) https://assets.ubuntu.com/ubuntu-server-guide

我没有办法在我的测试服务器上测试这一点,并希望在下一次发布时能够正确完成。

编辑:通过删除表达式的“行首”部分,事实证明我可以在我的测试服务器上调试它,该位置位于几个子目录中。通过 Muru 对答案的测试和我自己的测试,我对解决方案充满信心。

参考:
https://help.ubuntu.com/
https://code.launchpad.net/~ubuntu-core-doc/help.ubuntu.com/help.ubuntu.com
https://bazaar.launchpad.net/~ubuntu-core-doc/help.ubuntu.com/help.ubuntu.com/view/head:/.htaccess
Apache Web 服务器 - 如何删除语言扩展

答案1

RedirectMatch(和AliasMatch),正则表达式与 URL 匹配。它不必匹配整个 URL,除非使用^and进行锚定$。所以,是的,你的建议是:

RedirectMatch permanent ^/(stable|lts|20\.04)/(serverguide/.+\.html) https://ubuntu.com/server/docs
RedirectMatch permanent ^/(stable|lts|20\.04)/(serverguide/.+\.pdf) https://assets.ubuntu.com/ubuntu-server-guide

将匹配/20.04/serverguide/remote-administration.html.fr/20.04/serverguide/remote-administration.html。它还将匹配/20.04/serverguide/remote-administration.html.frfoobar,但同样,原始版本也是如此。

如果你已经安装了 Docker,测试它并不困难:

  1. 获取 docker 镜像和 Apache 配置:

    docker run --rm httpd:2.4-alpine cat /usr/local/apache2/conf/httpd.conf > my-httpd.conf
    
  2. 编辑my-httpd.conf以包含AllowOveride All<Directory "/usr/local/apache2/htdocs">块中
  3. 创建您的 htaccess 文件:

    mkdir foo
    cat > foo/.htaccess <<EOF
    RedirectMatch permanent ^/(stable|lts|20\.04)/(serverguide/.+\.html) https://ubuntu.com/server/docs
    RedirectMatch permanent ^/(stable|lts|20\.04)/(serverguide/.+\.pdf) https://assets.ubuntu.com/ubuntu-server-guide
    EOF
    
  4. 创建一些测试用例:

    cat > urls <<EOF
    localhost:8080/20.04/serverguide/remote-administration.html.fr
    localhost:8080/20.04/serverguide/remote-administration.html
    localhost:8080/20.04/serverguide/serverguide.pdf
    localhost:8080/20.04/serverguide/serverguide.pdf.fr
    localhost:8080/foo/20.04/serverguide/remote-administration.htmlbar
    EOF
    
  5. 运行apache:

    docker run -d --name apache -p 8080:80 -v $PWD/my-httpd.conf:/usr/local/apache2/conf/httpd.conf -v $PWD/foo:/usr/local/apache2/htdocs/ httpd:2.4-alpine
    
  6. 针对此本地 Apache 服务器测试您的 URL:

    % xargs -ta urls  -n1 curl -sI
    curl -sI localhost:8080/20.04/serverguide/remote-administration.html.fr
    HTTP/1.1 301 Moved Permanently
    Date: Thu, 23 Apr 2020 15:58:34 GMT
    Server: Apache/2.4.43 (Unix)
    Location: https://ubuntu.com/server/docs
    Content-Type: text/html; charset=iso-8859-1
    
    curl -sI localhost:8080/20.04/serverguide/remote-administration.html
    HTTP/1.1 301 Moved Permanently
    Date: Thu, 23 Apr 2020 15:58:34 GMT
    Server: Apache/2.4.43 (Unix)
    Location: https://ubuntu.com/server/docs
    Content-Type: text/html; charset=iso-8859-1
    
    curl -sI localhost:8080/20.04/serverguide/serverguide.pdf
    HTTP/1.1 301 Moved Permanently
    Date: Thu, 23 Apr 2020 15:58:34 GMT
    Server: Apache/2.4.43 (Unix)
    Location: https://assets.ubuntu.com/ubuntu-server-guide
    Content-Type: text/html; charset=iso-8859-1
    
    curl -sI localhost:8080/20.04/serverguide/serverguide.pdf.fr
    HTTP/1.1 301 Moved Permanently
    Date: Thu, 23 Apr 2020 15:58:34 GMT
    Server: Apache/2.4.43 (Unix)
    Location: https://assets.ubuntu.com/ubuntu-server-guide
    Content-Type: text/html; charset=iso-8859-1
    
    curl -sI localhost:8080/foo/20.04/serverguide/remote-administration.htmlbar
    HTTP/1.1 404 Not Found
    Date: Thu, 23 Apr 2020 15:58:34 GMT
    Server: Apache/2.4.43 (Unix)
    Content-Type: text/html; charset=iso-8859-1
    

相关内容