Score:0

Apache AliasMatch with Wildcard Not Working As Intended

vn flag

I am trying map all wildcards for a directory /a/* onto on file article.php using AliasMatch ^/a/(.*) /article.php but without redirecting (I want to keep the url looking the same). But I am getting a The requested URL was not found on this server. error.

Is AliasMatch even the right way to do this? Or is there a better way.

I am trying to achieve something like:

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

article.php

echo basename($_SERVER['REQUEST_URI'];

result:

hello
this-is-an-article

000-default.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>
Score:2
jp flag

The AliasMatch Directive;

Description: Maps URLs to filesystem locations using regular expressions

Syntax: AliasMatch regex file-path|directory-path

You are referencing an URL path instead of the required filesystem path; the file is not located at /article.php in you filesystem.

Try:

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

Furthermore, you are first redirecting from HTTP to HTTPS with mod_rewrite (you should use mod_alias instead), but your AliasMatch is in the HTTP <VirtualHost *:80>. It is never used.

Full fixed example configuration:

<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>
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.