0

I'm trying to configure mod_rewrite to attempt to load/serve files in a specific order. I think the easiest way to describe what I'm trying to achieve is with an example.

Let's say in my document root the following files/folders exist:

/missing.html
/subdomain.example.com/index.html
/subdomain.example.com/other_page.html
/folder1/index.html
/folder1/other_page.html

In order, the file return should be: 1) The folder/path requested 2) The folder/path requested, prepended with the hostname 3) The 'missing.html' file

For example:

http://example.com/folder1/ -> /folder1/index.html
http://example.com/folder1/other_page.html -> /folder1/other_page.html
http://example.com/folder1/nonexistant_file.html -> /missing.html
http://subdomain.example.com/ -> /subdomain.example.com/index.html
http://subdomain.example.com/other_page.html -> /subdomain.example.com/other_page.html 
http://subdomain.example.com/nonexistant_file.html -> /missing.html
http://some_other_subdomain.example.com/ -> /missing.html

So far I've managed to get this mostly working, but I'm having trouble with the 'missing.html' part. Anything that I would expect to be processed results in a 500 error.

I can't figure out if what I'm trying to achieve can be done with mod_rewrite alone.. the documentation doesn't make a huge amount of sense to me!

Here's my .htaccess so far:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule (.*) $1 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) %{HTTP_HOST}/$1 [L,QSA]

RewriteRule (.*) missing\.html [L]

Any input would be greatly appreciated!

Michael

1
  • Figured it out - see below
    – Michael
    Jul 13, 2016 at 1:19

1 Answer 1

0

This ruleset does what I was trying to accomplish

RewriteEngine On
RewriteBase /

# File exists? Do nothing!
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .* - [L,QSA]

# If we haven't redirected yet, perform internal redirect to /domain/
RewriteCond %{ENV:domain_rewritten} "!=1"
RewriteCond %{ENV:REDIRECT_domain_rewritten} "!=1"
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/%{HTTP_HOST} -d
RewriteRule (.*) %{HTTP_HOST}/%{REQUEST_URI} [L,QSA,E=domain_rewritten:1]

# Redirect above didn't work...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) missing\.html [L]

You must log in to answer this question.

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