سؤال

Morning,

I've got 2 entries in my .htaccess file, and I want to make them a little more dynamic.

Firstly I've got a non-www to www. redirect

 <IfModule mod_rewrite.c>
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
    RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
 </IfModule>

However, i'd like it to function conditionally as below

 <IfModule mod_rewrite.c>
    RewriteCond %{ENV:environment} != "dev" && != "local"
    RewriteCond %{HTTP_HOST} {DOESNT_CONTAIN .staging.com}
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
    RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
 </IfModule>

So basically, I want the www. redirect to kick in if the env variable doesnt equal dev or local, and the domain doesnt have .staging.com in it

How would I go about sorting this?

Secondly, this is pretty much an identical issue, I've got some authentication

Order deny,allow
Deny from all
AuthType Basic
AuthUserFile /var/www/vhosts/{HTTP_HOST}/httpdocs/.htpasswd
AuthName "Protected Area"
require valid-user
Allow from 127.0.0.1
Satisfy Any

I would like this to only run on the same conditions as above so: environment != "local" environment != "dev" url doesnt contain .staging.com

Hope some of you could shed some light onto this issue please.

Many thanks!

هل كانت مفيدة؟

المحلول

For the mod_rewrite part:

RewriteCond %{ENV:environment} != "dev" && != "local"
RewriteCond %{HTTP_HOST} {DOESNT_CONTAIN .staging.com}

would become:

RewriteCond %{ENV:environment} !dev 
RewriteCond %{ENV:environment} !local
RewriteCond %{HTTP_HOST} !\.staging\.com

For the mod_auth part, you'd need to set an env variable for the host and the dev/local:

SetEnvIfNoCase Host .staging.com noauth=true
SetEnvIf environment dev noauth=true
SetEnvIf environment local noauth=true

Then add additional Allow statement:

Allow from env=noauth
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top