troubleshooting Question

Update HTACCESS file using PHP

Avatar of ellandrd
ellandrdFlag for Ireland asked on
PHP
19 Comments1 Solution739 ViewsLast Modified:
I want to update my .htaccess file using PHP.

I have a section with comments and rules which I would like to read, update and then write back to the htaccess

So far I get read the contents, but all the contents get read.

How can i start reading the file contents at my comment, update the comment and write back to the htaccess?

My code so far:

$htaccess = $_SERVER['DOCUMENT_ROOT'].'/.htaccess';
             
       $content = file_get_contents($htaccess);
              
                $start = '# Restrict START #';
                  
                  $end = '# Restrict END #';
      
                  preg_match('/$start(.*)$end/s',$content,$matches);
                  
                  echo '<pre>';
                  print_r($matches);
                  echo '</pre>';
          

My .htaccess file contents:


# Restrict START #
order deny,allow
deny from all
allow from 127.0.0.1 # Localhost
# Restrict END #


My matches array is empty though
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 19 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 19 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros