1

I believe I want to know how to detect whether the first subdirectory in a request is valid.

I am wanting to deploy several different versions of the same web application into different directories just under root (say, by putting nothing in app folder except folders 'v01', 'v02',etc.). When the user goes to root I would like to load the latest approved version. However, for testing and other purposes I would like to have the option of specifying an earlier or later version manually.

For example, if I load www.example.com/app/people/mark.html a simple redirect would invisibly redirect to www.example.com/app/v02/people/mark.html. However, if I explicitly asked for www.example.com/app/v01/people/mark.html it would give me that page without redirect.

I can redirect to a subfolder no problem with htaccess in the app folder by doing something like

RewriteRule ^(.*)$  /v01/$1

However, the second part of not redirecting for old versions is causing me an issue. I could simply exempt all the other versions from a rewrite but this seems like a poor solution for various reasons (such as requiring explicit mods when a new version becomes outdated). Better, it seems like I would really want to detect if the first subfolder requested exists and then rewrite if it doesn't. So www.example.com/app/v01/people/mark.html would detect that www.example.com/app/v01/ is a valid directory and allow it to pass through to the main application. This seems superior to just !-d and !-f conditions because if I just check whether the file/directory exists in general then I can end up with the situation where a mistake in version 1 would get sent over to version 2.

For example, if I request www.example.com/app/v01/people/tom_oops.html instead of www.example.com/app/v01/people/tom.html this file won't exist and I won't want it to accidentally be redirected to www.example.com/app/v02/people/tom_oops.html by doing the following in htaccess:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$  /v02/$1

I've been playing with this for some time and am having issues in that 1) RewriteCond doesn't appear to have the functionality to extract this information and 2) the %{REQUIEST_URI} is slightly difficult to work with in RewriteCond because it returns /app/v02/blabla/whatever rather than v02/blabla/whatever.

Any suggestions would be great.

2
  • When I say redirect I do mean internal rewrite.
    – Steve
    May 25, 2015 at 17:01
  • Maybe just a typo in the question text, but that should be a $ not a % in the first RewriteRule pattern.
    – MrWhite
    May 26, 2015 at 20:11

1 Answer 1

3

I may be missing something, but my first reaction is to not redirect (or rather internally rewrite?) if the file doesn't exist, but only when the URL does not already contain the version folder (eg. v01).

For example, for .htaccess in the document root:

# Internally rewrite to the latest version if not specified
RewriteCond %{REQUEST_URI} !^/app/v\d\d/
RewriteRule ^app/(.*)$ /app/v02/$1 [L]

If you redirect only when the file doesn't exist then I would expect that something like tom_oops.html could result in a redirect loop?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.