## Redirects to supposrt 'short urls'.

## Thanks to Justin lurman for help with this:
## http://stackoverflow.com/questions/29758551/apache-redirects-with-different-destination-depending-on-whether-one-of-several

## Ideally we could replace bioconductor.org throughout with
## %{SERVER_NAME}:%{SERVER_PORT} but if we do that, CloudFront
## will redirect to master. ;-( 

## That means that if you are using master and you use a 
## 'short url', you will get redirected to plain old bioconductor.org.
## There seems to be no way around that, as far as I can tell 
## (at least at this point).

## Note that short urls don't work on mirrors because they
## may not be using apache and they don't copy this file in
## any case.

<IfModule mod_rewrite.c>

## define a variable that contains the protocol (http or https) of the
## current request.
## You can refer to the variable as  %{ENV:proto} and it will
## expand to its value.
## from http://stackoverflow.com/a/20419821/470769
# initialization code - put only once at the beginning of .htaccess
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ - [env=proto:https]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ - [env=proto:http]


##### Options -Indexes

RewriteEngine On
RewriteBase /packages/

RewriteRule ^/?$ %{ENV:proto}://bioconductor.org/packages/release/BiocViews.html#___Software [NE,R=302]

## Handle omitted trailing slash:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ %{ENV:proto}://bioconductor.org/packages/$1/ [L,R]

## Short urls for packages:
##
## (1) /packages/release/xxx or /packages/devel/xxx are internally rewritten 
##     to /packages/release/reposX/html/xxx.html or 
##     /packages/devel/reposX/html/xxx.html
##     Note: Repo search order is bioc -> workflows -> annotation -> experiment
##           where 'reposX' X is the first repo where xxx is found.

## packages/release/RSamtools -> /packages/release/bioc/html/Rsamtools.html
RewriteCond %{DOCUMENT_ROOT}/packages/$1/bioc/html/$2\.html -f
RewriteRule ^(release|devel|[.0-9]{3,4})/([^/]+)/?$ %{ENV:proto}://bioconductor.org/packages/$1/bioc/html/$2.html [R=302]

RewriteCond %{DOCUMENT_ROOT}/packages/$1/workflows/html/$2\.html -f
RewriteRule ^(release|devel|[.0-9]{3,4})/([^/]+)/?$ %{ENV:proto}://bioconductor.org/packages/$1/workflows/html/$2.html [R=302]

RewriteCond %{DOCUMENT_ROOT}/packages/$1/data/annotation/html/$2\.html -f
RewriteRule ^(release|devel|[.0-9]{3,4})/([^/]+)/?$ %{ENV:proto}://bioconductor.org/packages/$1/data/annotation/html/$2.html [R=302]

RewriteCond %{DOCUMENT_ROOT}/packages/$1/data/experiment/html/$2\.html -f
RewriteRule ^(release|devel|[.0-9]{3,4})/([^/]+)/?$ %{ENV:proto}://bioconductor.org/packages/$1/data/experiment/html/$2.html [R=302]

## (2) /packages/xxx is internally rewritten to
##     /packages/release/reposX/html/xxx.html or 
##     /packages/devel/reposX/html/xxx.html
##     Note: Release repos are searched first, then devel.
##           Repo search order is bioc -> workflows -> annotation -> experiment
##           where 'reposX' X is the first repo where xxx is found.

RewriteCond %{DOCUMENT_ROOT}/packages/release/bioc/html/$1\.html -f
RewriteRule ^([^/]+)/?$ %{ENV:proto}://bioconductor.org/packages/release/bioc/html/$1.html [R=302]

RewriteCond %{DOCUMENT_ROOT}/packages/release/workflows/html/$1\.html -f
RewriteRule ^([^/]+)/?$ %{ENV:proto}://bioconductor.org/packages/release/workflows/html/$1.html [R=302]

RewriteCond %{DOCUMENT_ROOT}/packages/release/data/annotation/html/$1\.html -f
RewriteRule ^([^/]+)/?$ %{ENV:proto}://bioconductor.org/packages/release/data/annotation/html/$1.html [R=302]

RewriteCond %{DOCUMENT_ROOT}/packages/release/data/experiment/html/$1\.html -f
RewriteRule ^([^/]+)/?$ %{ENV:proto}://bioconductor.org/packages/release/data/experiment/html/$1.html [R=302]

RewriteCond %{DOCUMENT_ROOT}/packages/devel/bioc/html/$1\.html -f
RewriteRule ^([^/]+)/?$ %{ENV:proto}://bioconductor.org/packages/devel/bioc/html/$1.html [R=302]

RewriteCond %{DOCUMENT_ROOT}/packages/devel/workflows/html/$1\.html -f
RewriteRule ^([^/]+)/?$ %{ENV:proto}://bioconductor.org/packages/devel/workflows/html/$1.html [R=302]

RewriteCond %{DOCUMENT_ROOT}/packages/devel/data/annotation/html/$1\.html -f
RewriteRule ^([^/]+)/?$ %{ENV:proto}://bioconductor.org/packages/devel/data/annotation/html/$1.html [R=302]

RewriteCond %{DOCUMENT_ROOT}/packages/devel/data/experiment/html/$1\.html -f
RewriteRule ^([^/]+)/?$ %{ENV:proto}://bioconductor.org/packages/devel/data/experiment/html/$1.html [R=302]

RewriteCond %{DOCUMENT_ROOT}/packages/$1 !-d 
RewriteRule ^([^/]+)/?$ %{ENV:proto}://bioconductor.org/about/removed-packages/ [R=302]

## Short urls for vignettes:
##
## (1) /packages/<release or devel>/xxx/vignettes/yyy is internally rewritten to
##     /packages/<release or devel>/reposX/vignettes/xxx/inst/doc/yyy
##     Note: Repo search order is bioc -> workflows -> annotation -> experiment
##           'reposX' X is the first repo where xxx is found
##           'xxx' is the package name
##           'yyy' is the vignette file name with extension

RewriteCond %{DOCUMENT_ROOT}/packages/$1/bioc/vignettes/$2/inst/doc/$3 -f
RewriteRule ^(release|devel|[.0-9]{3,4})/([^/]+)/vignettes/([^/]+)/?$ %{ENV:proto}://bioconductor.org/packages/$1/bioc/vignettes/$2/inst/doc/$3 [R=302]

RewriteCond %{DOCUMENT_ROOT}/packages/$1/workflows/vignettes/$2/inst/doc/$3 -f
RewriteRule ^(release|devel|[.0-9]{3,4})/([^/]+)/vignettes/([^/]+)/?$ %{ENV:proto}://bioconductor.org/packages/$1/workflows/vignettes/$2/inst/doc/$3 [R=302]

RewriteCond %{DOCUMENT_ROOT}/packages/$1/data/annotation/vignettes/$2/inst/doc/$3 -f
RewriteRule ^(release|devel|[.0-9]{3,4})/([^/]+)/vignettes/([^/]+)/?$ %{ENV:proto}://bioconductor.org/packages/$1/data/annotation/vignettes/$2/inst/doc/$3 [R=302]

RewriteCond %{DOCUMENT_ROOT}/packages/$1/data/experiment/vignettes/$2/inst/doc/$3 -f
RewriteRule ^(release|devel|[.0-9]{3,4})/([^/]+)/vignettes/([^/]+)/?$ %{ENV:proto}://bioconductor.org/packages/$1/data/experiment/vignettes/$2/inst/doc/$3 [R=302]

## (2) /packages/vignettes/xxx/yyy is internally rewritten to
##     /packages/<release or devel>/reposX/vignettes/xxx/inst/doc/yyy
##     Release repos are searched first, then devel.
##     Note: Repo search order is bioc -> workflows -> annotation -> experiment
##           where 'reposX' X is the first repo where xxx is found.
##           'reposX' X is the first repo where xxx is found
##           'xxx' is the package name
##           'yyy' is the vignette file name with extension

RewriteCond %{DOCUMENT_ROOT}/packages/release/bioc/vignettes/$1/inst/doc/$2 -f
RewriteRule ^([^/]+)/vignettes/([^/]+)/?$ %{ENV:proto}://bioconductor.org/packages/release/bioc/vignettes/$1/inst/doc/$2 [R=302]

RewriteCond %{DOCUMENT_ROOT}/packages/release/workflows/vignettes/$1/inst/doc/$2 -f
RewriteRule ^([^/]+)/vignettes/([^/]+)/?$ %{ENV:proto}://bioconductor.org/packages/release/workflows/vignettes/$1/inst/doc/$2 [R=302]

RewriteCond %{DOCUMENT_ROOT}/packages/release/data/annotation/vignettes/$1/inst/doc/$2 -f
RewriteRule ^([^/]+)/vignettes/([^/]+)/?$ %{ENV:proto}://bioconductor.org/packages/release/data/annotation/vignettes/$1/inst/doc/$2 [R=302]

RewriteCond %{DOCUMENT_ROOT}/packages/release/data/experiment/vignettes/$1/inst/doc/$2 -f
RewriteRule ^([^/]+)/vignettes/([^/]+)/?$ %{ENV:proto}://bioconductor.org/packages/release/data/experiment/vignettes/$1/inst/doc/$2 [R=302]

RewriteCond %{DOCUMENT_ROOT}/packages/devel/bioc/vignettes/$1/inst/doc/$2 -f
RewriteRule ^([^/]+)/vignettes/([^/]+)/?$ %{ENV:proto}://bioconductor.org/packages/devel/bioc/vignettes/$1/inst/doc/$2 [R=302]

RewriteCond %{DOCUMENT_ROOT}/packages/devel/workflows/vignettes/$1/inst/doc/$2 -f
RewriteRule ^([^/]+)/vignettes/([^/]+)/?$ %{ENV:proto}://bioconductor.org/packages/devel/workflows/vignettes/$1/inst/doc/$2 [R=302]

RewriteCond %{DOCUMENT_ROOT}/packages/devel/data/annotation/vignettes/$1/inst/doc/$2 -f
RewriteRule ^([^/]+)/vignettes/([^/]+)/?$ %{ENV:proto}://bioconductor.org/packages/devel/data/annotation/vignettes/$1/inst/doc/$2 [R=302]

RewriteCond %{DOCUMENT_ROOT}/packages/devel/data/experiment/vignettes/$1/inst/doc/$2 -f
RewriteRule ^([^/]+)/vignettes/([^/]+)/?$ %{ENV:proto}://bioconductor.org/packages/devel/data/experiment/vignettes/$1/inst/doc/$2 [R=302]

