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?
htaccess rewrite rules can be used to direct requests for one subdirectory to a different location, such as an alternative subdirectory or even the domain root. In this example, requests to http://mydomain.com/folder1/ will be automatically redirected to http://mydomain.com/folder2/.
F|forbidden The following rule will forbid .exe files from being downloaded from your server. RewriteRule "\.exe" "-" [F] This example uses the "-" syntax for the rewrite target, which means that the requested URI is not modified. There's no reason to rewrite to another URI, if you're going to forbid the request.
QSA means that if there's a query string passed with the original URL, it will be appended to the rewrite ( olle? p=1 will be rewritten as index.
The %{ENV:variable} form of TestString in the RewriteCond allows mod_rewrite's rewrite engine to make decisions conditional on environment variables. Note that the variables accessible in mod_rewrite without the ENV: prefix are not actually environment variables.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With