Jump to content

Read htaccess commands by php and apply it


Issam

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by Issam
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.