0

I have to add a rule in a server that host many sites. This rule must be added only in those .htaccess files that belongs to a site with domain beggining with "dev-" or "review-".

I have this:

#!/bin/bash

HOMEFOLDER=/home/*
FILE='public_html/.htaccess'

for f in $HOMEFOLDER;
do
  # Only enter to folders
  if [ -d "$f" ]
  then
    cd "$f"
    echo ">>>> $f"
    then
      echo ".htaccess file not found!"
    else

      if grep -Fxq "# rule 01312019" $FILE
      then
        echo "Rule 01312019 already added!"
      else
        # Back up .htaccess
        TODAY=$(date +"%Y%m%d%H%M%S")

        echo "Back up .htaccess"
        mkdir -p backup
        # Save a timestamped copy in case of multiple script runs.
        cp $FILE backup/.htaccess.$TODAY
        # Same name copy for easy rollback.
        cp $FILE backup/.htaccess

        # Add rule 01312019
        echo "Adding rule 01312019"
        echo "++++++"

        echo "# rule 01312019" >> $FILE
        echo 'SetEnvIf Host "^dev-|^review-" INTERNAL' >> $FILE
        echo "# set header only if internal domain" >> $FILE
        echo "Header set X-Robots-Tag "noindex, nofollow, noarchive" env=INTERNAL" >> $FILE
      fi
    fi
    echo ""
  fi
done;

I know the regular expression is not ok. I need the correct way to write it.

Thank you.

  • So, your question is about the BASH script, not `.htaccess`? – MrWhite Feb 01 '19 at 00:48
  • More about regular expressions to match urls in .htaccess. – Ricardo Gamarra Feb 01 '19 at 00:56
  • I'm writing a bash script just because it's one server with multiple sites. – Ricardo Gamarra Feb 01 '19 at 00:57
  • Is the relevant part of your script this line... `SetEnvIf Host "^dev-|^review-" INTERNAL`? Is that the regex you are asking about? But that regex looks OK (it could be "improved", but otherwise it's OK). (However, this isn't really what your question is asking? If it was then the "Bash" part of your question is seemingly irrelevant?) – MrWhite Feb 01 '19 at 01:07
  • Exactly. I'm only asking for that line. Sorry I didn't explain myself well. I need to write the regexp correctly. – Ricardo Gamarra Feb 01 '19 at 01:14
  • So, what actually is the problem? "I know the regular expression is not ok." - What makes you think its not ok? If you are trying to set the `INTERNAL` environment variable to `1` when the `Host` header starts with either `dev-` or `review-` then that directive should work OK. There is nothing wrong with the regexp. (?) – MrWhite Feb 01 '19 at 08:59
  • So for some other reason is not working... thanks anyway. – Ricardo Gamarra Feb 01 '19 at 12:30
  • Yes, it would seem so. In order to debug the `.htaccess` file, we would need to see the `.htaccess` file, not the script that generates that file. Presumably the `.htaccess` file is generated OK? Do you have other directives in the `.htaccess` file? Are `.htaccess` files enabled? etc. – MrWhite Feb 01 '19 at 14:43
  • 1
    You were right @MrWhite. I don't know the reason, but now the requests to the sites are responding with X-Robots-Tag Http Headers as expected. I think maybe it's something about permissions, because I did some changes related to that in a while, tired of trying. Thank you anyway. – Ricardo Gamarra Feb 04 '19 at 20:58

0 Answers0