Monday, April 9, 2018

Htaccess - Skip redirect if env var is not set

Leave a Comment

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?

1 Answers

Answers 1

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] 

What it does it checks on every run if prefixed the prefixed env var is already set and re adds the environment variable without the REDIRECT_ prefix.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment