Apemantus
Apemantus

Reputation: 683

Change DocumentRoot in Apache depending on URL path

I'm redoing my API. Fortunately, its versioned. Yay! But what's the best way to handle the new code.

Basically; I have api.example.com/v1/method etc. My site runs on apache, and api.example.com points to /var/www/html/api.example.com

For the new API, I will be using a completely different framework and codebase etc, so I don't want to mix up the files.

Ideally, what I want is api.example.com/v1/ to point to /var/www/html/api-v1.example.com and api.example.com/v2/ to point to /var/www/html/api-v2.example.com.

[Edited, I originally said subdirectories which I think is confusing, but really I think I want them as completely separate sites]

Is this possible using Apache?

Additional question: if instead of using /v1/ /v2/ etc, I versioned my API by using an Accept header like:

Accept: application/vnd.api-example-com-v2+json

how would I map that to a different directory?

Upvotes: 2

Views: 2549

Answers (1)

Jon Lin
Jon Lin

Reputation: 143886

It's fine if you have your document root setup as

/var/www/html/api.example.com

Then have the directories /v1/ and /v2/ inside the document root. As for the Accept header. You can match against that using mod_rewrite:

RewriteEngine On

RewriteCond %{HTTP_ACCEPT} application/vnd.api-example-com-(.*)\+json
RewriteCond %1::$1 !^(.*)::\1
RewriteRule ^(.*)$ /%1/$1 [L]

If that's what you want, then you need to create some aliases in your vhost/server config:

AliasMatch ^/v([0-9]+)/(.*)$  /var/www/html/api-v$1.example.com/$2

Upvotes: 1

Related Questions