1

It is possible write any kind of condition (IF) in .htaccess to apply some specific actions ?

#if hostname = xxx then
php_flag  display_errors                  on
php_flag  display_startup_errors          on
#end if

1 Answer 1

0

Yes, you can use Apache Expressions on Apache 2.4+.

For example:

<If "%{HTTP_HOST} == 'xxx'">
php_flag  display_errors                  on
php_flag  display_startup_errors          on
</If>

UPDATE: is possible use like/contains in operator? Something like HTTP_HOST like *.test.mydomain.com ?

You can use various operators. For that example, you probably want to use a regular expression (using the =~ operator). For example:

<If "%{HTTP_HOST} =~ m#\.test\.mydomain\.com$#">
:
</If>
7
  • I think...it is possible use like/contains in operator ? Something like HTTP_HOST like '*.test.mydomain.com' ?
    – MCunha98
    Feb 19, 2020 at 14:47
  • There are various operators that can be used in an Apache Expression. If you want a "like" or "contains" type comparison then you are probably after a regular expression (regex). I've updated my answer.
    – MrWhite
    Feb 19, 2020 at 16:05
  • Thanks @MrWhite, I did some tests on my development environment and it works fine , but my problem now is other. On Apache 2.4 the syntax like XXX =~ /expression/ work, but my production still Apache 2.2, and when I use it the server simply crash, probably not supported on 2.2
    – MCunha98
    Feb 19, 2020 at 23:27
  • Yes, Apache Expressions (as discussed above) are a new feature of Apache 2.4, they are not available on Apache 2.2. If you are still on Apache 2.2 then you can't do this in .htaccess alone... you will need access to the server config to proceed further (in order to Define a variable that can be checked later in .htaccess). However, depending on how your vHosts are configured, this may not be practical. It may be preferable to define these "PHP settings" in your PHP code itself.
    – MrWhite
    Feb 20, 2020 at 0:10
  • Thanks again @MrWhite, the problem in 2.2 is if I leave the PHP resolve the problem and show exceptions, unfortunatelly I receive only a 500 internal server error on screen and none event is recorded on log. In 2.4 I can force the server to show the error on screen/intercept using custom error handler code that I wrote but in 2.2 I'm totally blind. Well....again, thanks for your time to reply and clarify the points
    – MCunha98
    Feb 20, 2020 at 12:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .