带有通配符的 Apache AliasMatch 未按预期工作

带有通配符的 Apache AliasMatch 未按预期工作

我正在尝试将目录中的所有通配符映射/a/*到文件上article.phpAliasMatch ^/a/(.*) /article.php但不进行重定向(我希望保持 URL 看起来相同)。但我收到错误The requested URL was not found on this server.

别名匹配这是正确的做法吗?或者有更好的方法吗?

我正在尝试实现如下目标:

example.com/a/hello
example.com/a/this-is-an-article

文章.php

echo basename($_SERVER['REQUEST_URI'];

结果

hello
this-is-an-article

000-默认.conf

<VirtualHost *:80>
 ServerAdmin webmaster@localhost
 DocumentRoot /var/www/html
 ErrorLog ${APACHE_LOG_DIR}/error.log
 CustomLog ${APACHE_LOG_DIR}/access.log combined
 RewriteEngine on
 RewriteCond %{HTTPS} off
 RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}
 RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
 RewriteRule ^ https://%1%{REQUEST_URI} [END,L,R=permanent]
 AliasMatch ^/a/(.*) /article.php
</VirtualHost>
<Directory /var/www>
 Options Indexes FollowSymLinks
 AllowOverride All
 Require all granted
</Directory>

答案1

AliasMatch指示

描述:使用正则表达式将 URL 映射到文件系统位置

句法: AliasMatch regex file-path|directory-path

您引用的是 URL 路径,而不是所需的文件系统路径;该文件不在/article.php您的文件系统中。

尝试:

AliasMatch "^/a/(.*)" "/var/www/html/article.php"

此外,您首先使用 mod_rewrite 从 HTTP 重定向到 HTTPS(你应该使用 mod_alias),但您的AliasMatch是在 HTTP 中<VirtualHost *:80>。它从未被使用过。

完整固定示例配置:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    Redirect permanent / https://example.com/
</VirtualHost>

<VirtualHost *:443>
    ServerName www.example.com
    Redirect permanent / https://example.com/

    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>

<VirtualHost *:443>
    ServerName example.com
    DocumentRoot /var/www/html
    AliasMatch "^/a/(.*)" "/var/www/html/article.php"

    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>

相关内容