How to add line using in .htaccess file using PERL ?


I would like to request an assistant.

May i know how to add line in .htaccess file using PERL if the line is NOT ALREADY EXIST in the file ?


The filename is /home/$username/public_html/.htaccess

I would like to add the following line in it :

suPHP_ConfigPath /home/$username/public_html
<Files php.ini>
order allow,deny
deny from all

But the adding is only IF the above line is not already in the .htaccess .

Appreciates of any assistant.

Thank you.

Shamsul KamalJunior TechAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


The code provided should do the work.

Usage : /home/$username/public_html/.htaccess suPHP_ConfigPath

Best Regards


if(-e $ARGV[0])

    print 'File does not exist.\n';

open(INPUT,$ARGV[0]) or die 'Cannot open file: $!\n';



    if (my ($found) = m/($ARGV[1])/o) {


close INPUT;

if ($exist_in_file == 1) {
print "found $ARGV[1] in $ARGV[0]\n";
#Do nothing because the content exist in the file
	print "not found $ARGV[1] in $ARGV[0]";
	#Put the content in the file (at the end)
	print " the file for write\n";
	open(OUTPUT, ">>$ARGV[0]");
	print OUTPUT "\nsuPHP_ConfigPath /home/";
	print OUTPUT '$username';
	print OUTPUT "/public_html\n";
	print OUTPUT "<Files php.ini>\n";
	print OUTPUT "order allow,deny\n";
	print OUTPUT "deny from all\n";
	print OUTPUT "</Files>\n";
	close (OUTPUT); 

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.