0

I'm trying to set up apache with an alias with and rewrite rules.

I have rewrite set up to forward all requests to index.php. That works great.

My issue is with setting up the AliasMatch for some specific locations.

If the client browses to /core/plugins/<plugin_name>/client, I want the alias to map the directory to /var/www/example.com/src/plugins/<plugin_name>/client on the server

The plugin name is variable and may or may not exist.

Here is my apache vhost config:

ServerName example.com
DocumentRoot /var/www/example.com/src/public

AliasMatch ^/core/plugins/(.+)/client /var/www/example.com/src/plugins/$1/client

<Directory /var/www/example.com/src/public>
    Require all granted
    Options -Indexes

    AddDefaultCharset UTF-8

    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-f

    RewriteRule ^(.+)$ index.php?url=$1 [QSA,PT]
    RewriteBase /
</Directory>

When I browse to /core/plugins/plugin1/client/style.css, the file served should theoretically be /var/www/example.com/src/plugins/plugin1/client/style.css, but instead I just get a redirect loop that adds /index.html repeatedly until my browser hangs for too many redirects. I'm not even referencing index.html in my config.

Grant Martin
  • 125
  • 1
  • 2
  • 16
  • I think you've misunderstood use of `Alias` and `AliasMatch` here. Target of both directives is an **existing** filesystem path outside `DocumentRoot` – anubhava Mar 30 '18 at 18:27
  • the target SHOULd exist, but may not. If that's the case the rewrite rule should take over and just serve index.php. What I meant by may or may not exists, is I cannot account for plugin names in the apache config, but I can account for where they will be. – Grant Martin Mar 30 '18 at 20:01
  • Your rewrite rules will only work for the `DocumentRoot /var/www/example.com/src/public` not for the paths outside `DocumentRoot` – anubhava Mar 30 '18 at 20:04
  • The alias should add it though so the rewrite engine sees it as in the document, no? My alias works as I want it if I hard code a plugin name, but what I want is to use a variable in the alias path to account for whatever plugin name is given. – Grant Martin Mar 30 '18 at 20:06

1 Answers1

0

I was able to solve my issue by using the following AliasMatch rule:

AliasMatch "^/core/plugins/([^\/]+)/([^\/]+)/(.*)" "/var/www/example.com/src/plugins/$1/client/$2/$3"

An explanation of what this is doing in my case -

The user inputs: /core/plugins/plugin1/css/style.css

The first argument of the Alias rule, "^/core/plugins/([^\/]+)/([^\/]+)/(.*)", checks the user input and assigns $1 to the output of ([^\/]+) which is plugin1 in this case, $2 to the output of ([^\/]+) which is css in this case, and $3 to (.*) which is everything left in the url after $2/, style.css in this case. These variables $1, $2, and $3 can be used to map the path in the second argument. In my case "/var/www/example.com/src/plugins/$1/client/$2/$3"

Think of the regex output as /core/plugins/$1/$2/$3 with reference to the second argument.

So, based on the user input /core/plugins/plugin1/css/style.css, the actual file mapped will be /var/www/example.com/src/plugins/plugin1/client/css/style.css

I hope this helps people that are running into this issue.

Grant Martin
  • 125
  • 1
  • 2
  • 16