7

I simply want to skip a redirect done with a "RewriteRule" when a specific env variable is not set by a former "RewriteRule".

Here is my example which doesn't work.

RewriteEngine on
RewriteBase /

#
# if /internal ... set ENV var INTERNALAREA = true
#
RewriteRule ^internal(.*)$ - [E=INTERNALAREA:true]

#
# Redirect to /maintenance.php if not in internal area and not already at /maintenance.php
#
RewriteCond %{ENV:INTERNALAREA} !true
RewriteCond %{REQUEST_URI} !^/maintenance.php$
RewriteRule ^(.*)$ /maintenance.php [R=302,L]

#
# Default TYPO3 rewrites
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)\.(\d+)\.(php|js|css|png|jpg|gif|gzip)$ $1.$3 [L]

RewriteRule ^fileadmin/(.*/)?_recycler_/ - [F]
RewriteRule ^fileadmin/templates/.*(\.txt|\.ts)$ - [F]
RewriteRule ^typo3conf/ext/[^/]+/Resources/Private/ - [F]
RewriteRule ^(typo3/|t3lib/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon\.ico) - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]

In the maintenance PHP I have a phpinfo() which outputs $_SERVER['REDIRECT_REDIRECT_INTERNALAREA'] = true.

I'm always getting redirected to the maintenance.php when accessing /internal/....

What am I doing wrong?

denis
  • 1,393
  • 3
  • 14
  • 34

1 Answers1

10

I have found the answer to my question. The problem has its roots at the following question:

When setting environment variables in Apache RewriteRule directives, what causes the variable name to be prefixed with "REDIRECT_"?

When now setting the following before all the other rules everything does work as expected:

RewriteCond %{ENV:REDIRECT_INTERNALAREA} (.+)
RewriteRule .* - [E=INTERNALAREA:%1]

Here is how it works. It checks on every run if the prefixed env var is already set and re adds the environment variable without the REDIRECT_ prefix.

denis
  • 1,393
  • 3
  • 14
  • 34