Issam Posted October 9, 2014 Share Posted October 9, 2014 Hi; I would like to make a php script read the commands in the htaccess file and apply those commands to itself. Here is an example : htaccess commands : RewriteEngine On RewriteCond %{QUERY_STRING} badword RewriteRule .* - [F] The meaning : Deny any request in witch the query string contain the word badword The translation into php code : if( strpos($_SERVER['QUERY_STRING'], 'badword') !== false ) { header('HTTP/1.1 500 Internal Server Error'); exit(0); } Now this example is simple, but how to translate more complex htaccess commands with multiple RewriteCond and complex regular expressions into php commands like the one i did ? or better is there any php script that read an entire htaccess file and apply the rule it read on it ? Thank you Quote Link to comment Share on other sites More sharing options...
jcbones Posted October 9, 2014 Share Posted October 9, 2014 I'm sure you could. Never tried it, don't see the point. .htaccess is Apache file, there is no need for PHP to see or read it. IMO, there is no need to apply an Apache rule to PHP, unless, you are doing things in Apache that is better left to PHP. Perhaps you could grab the file with file_get_contents(), or if you need it in an array file(). The parsing would be a custom script. Quote Link to comment Share on other sites More sharing options...
Issam Posted October 10, 2014 Author Share Posted October 10, 2014 (edited) Hi jcbones, there is some cases in witch an htaccess rules can't coincide with each other or my knowledge isn't capable to find an htaccess solution to it. Here is an example for possible htaccess scenario : I have two .htaccess portions that i want to add both of them into one .htaccess file, the first portion can't be modified and its content is this one : RewriteCond %{REQUEST_FILENAME} \.php$ RewriteCond %{REQUEST_FILENAME} !/index[23]?\.php$ RewriteCond %{REQUEST_FILENAME} -f RewriteRule ^([^/]+/)*([^/.]+\.)+php$ - [F] The second portion is this one, but the flags can be modified to resolve this issue of course : RewriteRule (.*) /var/www/html/script.php [L,QSA] Now here is the problem, if i add the first portion before the second, the .htaccess will deny access to script.php because the first portion deny access to any filename different than index.php Now, if i add the second portion before the first portion, the first portion won't be read by the server because the second portion will redirect to script.php giving no chance to read the rest of the file and apply any other rule. So that is the cause that make me think to convert it to php code to resolve this kind of situations. Regards Edited October 10, 2014 by Issam Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.