0

Is there a way to perform an .htaccess edit in bulk through ssh?

I have multiple websites which goes like this:

/home/user/website1/public_html/.htaccess
/home/user/website2/public_html/.htaccess
...

And would like to add (not just replace the whole .htaccess ) at bottom of the file couple new lines, such as:

<FilesMatch ".+\.php$">
Require all denied
</FilesMatch>

Is there a way to do that?

0

Yes, there is. You only need to write some codes and run it on the server after ssh. For example, in Python:

# The lines you want to append
lines="""
<FilesMatch ".+\.php$">
Require all denied
</FilesMatch>
"""

# The files need to append
files=[]
files.append('/home/user/website1/public_html/.htaccess')
files.append('/home/user/website2/public_html/.htaccess')

# Append lines to files
for file in files:
    f=open(file,'ab')
    f.write(lines)
    f.close()
0

Another option, using a shell script, would be:

#!/bin/sh
for htaccess in home/user/website*/public_html/.htaccess
do
  cat >> "$htaccess" << 'EOF'
<FilesMatch ".+\.php$">
Require all denied
</FilesMatch>
EOF
done

The script loops over every directory under /home/user that starts with "website" and appends the desired lines to the public_html/.htaccess file. I use a quoted here-document so that none of the contents are "helpfully" interpreted by the shell.

If you want to have a blank line separating the previous contents from the additions, simply insert it before the <Filesmatch portion that you're inserting (immediately after the cat line).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.