Htaccess if module php

htaccess if statement

This works great in the live environment but doesn’t apply to the test environment and breaks the site. Is there a way I can put it in an if statement or something by IP of the server or URL of website or something so that it only comes into effect in the live environment?

What’s the difference between your live and test environments? How are they configured in Apache? And most importantly, how does it break? What’s in the error_log?

2 Answers 2

With Apache 2.4, it is easy with / directives (on % ?).

 == 'foo'"> # configuration for foo # default configuration 

For Apache 2.2 and earlier, I would add a parameter to the startup command line of Apache ( -D option) in one of the two environments then test if it is present or not via .

To do this on Windows, with Apache started as a service, modify key registry HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Apache2.\ImagePath
by appending -DFOO . Then, you can write:

 # configuration for foo # default configuration 

Another alternative you can do is to change httpd.conf in the test environment to use «.htaccess-test» instead of «.htaccess«.

This is simply done by modifying httpd.conf and adding the following line outside of any block:

AccessFileName .htaccess-test 

NOTE: You can add AccessFileName inside a block if you want to apply it to a specific VirtualHost.

What this means is that the test environment will use .htaccess-test while the production environment will use .htaccess. Hence, you get the freedom of configuring each environment separately.

Then create a file named .htaccess-test adjacent to .htaccess. Modify .htaccess-test with your test configuration, then finally restart Apache Web server in the test environment server.

Источник

Conditional php_flag statements in .htaccess

Is there a way to conditionally execute php_flag statements in .htaccess? Here are two things I’m trying to do: Turn error reporting on if the client’s IP address matches the IP address I use:

if % == '12.34.56.78' then php_flag error_reporting 1 else php_flag error_reporting 0 

Turn off register_globals if the IP address matches mine, so that I can debug any issues caused by the code expecting this turned on.

if % == '12.34.56.78' then php_flag register_globals on else php_flag register_globals on 

1 Answer 1

if % == '12.34.56.78' then php_flag error_reporting 1 else php_flag error_reporting 0 

This is only really possible with the advent of Apache Expressions in Apache 2.4+, where you can do just this sort of thing.

 == '12.34.56.78'"> php_flag error_reporting 1 php_flag error_reporting 0 

If on Apache 2.2 this is not really possible, especially with something that is dependent on the request, like the REMOTE_ADDR . On Apache 2.2 you have , but this only tests the existence of parameters that have been defined on the Appache command line with the -D option. So, this is really a per-server configuration switch. (On Apache 2.4 you can use the Define directive in the server config to define parameters somewhat conditionally, however, this still can’t be defined based on elements of the request AFAIK. Besides, you have the use of Apache Expressions on Apache 2.4 anyway.)

Источник

How to write «if. else» mod_rewrite statements in .htaccess

I’ve scoured StackOverflow for an answer to this and seen many questions come close, but nothing is working for me yet. I’d like to have my .htaccess apply different rules based on the domain calling it. Basically, if subdomain.example.com is hit, redirect to google.com. If localhost is hit, redirect to yahoo.com. If www.example.com (or example.com) is hit, redirect to bing.com. I’ll be adding in actual rewrite rules (setting environment variables and htpasswd protection) later, but first I need to get the «if then» part working. My (non-working) code is below. Any help is greatly appreciated.

RewriteEngine On RewriteCond % ^subdomain\.example\.com$ RewriteRule http://google.com/ [L,R=301] RewriteCond % ^localhost$ RewriteRule http://yahoo.com/ [L,R=301] RewriteCond % ^(www\.)?example\.com$ RewriteRule http://bing.com/ [L,R=301] 
RewriteEngine On RewriteCond % ^subdomain\.domain\.com$ RewriteCond % !-f RewriteCond % !-d RewriteRule .? - [S=1] RewriteRule ^(.*)$ /index.php/$1 [L] RewriteCond % ^localhost$ RewriteCond % !-f RewriteCond % !-d RewriteRule .? - [S=7] RewriteRule ^(.*)$ /subdir/www/index.php/$1 [L] RewriteRule ^ - [E=MYSQL_DB_HOST:localhost] RewriteRule ^ - [E=MYSQL_DB_NAME:XXX] RewriteRule ^ - [E=MYSQL_USERNAME:XXX] RewriteRule ^ - [E=MYSQL_PASSWORD:XXX] RewriteRule ^ - [E=BASE_URL:XXX] RewriteRule ^ - [E=ENVIRONMENT:XXX] RewriteCond % ^example\.com$ RewriteCond % !-f RewriteCond % !-d RewriteRule .? - [S=1] RewriteRule ^(.*)$ /index.php/$1 [L] 

Источник

Читайте также:  Getelement by class javascript
Оцените статью