23tux 23tux - 1 month ago 14
Apache Configuration Question

Match URL to a specific file in Apache

I have a domain which is currently redirecting to another page. This is my virtual hosts file:

<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com

Redirect permanent / http://www.anotherdomain.com
</VirtualHost>


Now I need an SSL certificate for that page, and to verify I must provide a file under a certain URL.

Is it possible to provide the apache configuration that matches e.g. example.com/verify_file_123.txt to a file on my filesystem, e.g. /var/www/my_site/verify_file_123.txt while still having redirects for all other routes?

I tried it with

<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com

Alias "/verify_file_123.txt" "/var/www/my_site/verify_file_123.txt"

Redirect permanent / http://www.anotherdomain.com
</VirtualHost>


but that doesn't work.

Answer

Redirects are processed before Aliases are processed, and therefore a request that matches a Redirect or RedirectMatch will never have Aliases applied.

You can try something like this:

Alias "/verify_file_123.txt" "/var/www/my_site/verify_file_123.txt"

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/verify_file_123.txt
RewriteRule ^ http://www.newdomain.com%{REQUEST_URI} [R=301,L]
Comments