Mobile app version of vmapp.org
Login or Join
Margaret670

: Htaccess condition if the first subfolder exists 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

@Margaret670

Posted in: #Htaccess #ModRewrite

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 example.com/app/people/mark.html a simple redirect would invisibly redirect to example.com/app/v02/people/mark.html. However, if I explicitly asked for 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/


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 example.com/app/v01/people/mark.html would detect that 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 example.com/app/v01/people/tom_oops.html instead of example.com/app/v01/people/tom.html this file won't exist and I won't want it to accidentally be redirected to example.com/app/v02/people/tom_oops.html by doing the following in htaccess:

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


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.

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Margaret670

1 Comments

Sorted by latest first Latest Oldest Best

 

@Jamie184

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/vdd/
RewriteRule ^app/(.*)$ /app/v02/ [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?

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme