0

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>

1 Answer 1

2

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>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .