Skip to content

Instantly share code, notes, and snippets.

@Jeff-Russ
Last active January 29, 2017 07:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jeff-Russ/28cb67ad503d0e207033f97674b5399f to your computer and use it in GitHub Desktop.
Save Jeff-Russ/28cb67ad503d0e207033f97674b5399f to your computer and use it in GitHub Desktop.
.htaccess file generators
#!/bin/bash
# DIRECTIONS: run these:
# $ chmod u+x ./htaccess.sh
# $ source ./htaccess.sh
# THEN navigate anywhere and run the functions as
# if they are built in commands. See below for more.
#################################################################
# Write .htaccess file(s) which limit navigation from url not
# on current domain and with certain path (or none if blank).
# All arguments up to and not including '-o' are the portion after
# the site's host domain extension, ie after .com/
# All arguments after -o are destination directories to place
# .htaccess files. A warning is issued before overwriting any
# .htaccess if found.
htaccess_allow_refer() {
local wd="$(pwd)"
local webpaths='('
for arg in "$@"; do
if [[ "$arg" == -i* ]]; then shift;
elif [[ "$arg" == -o* ]]; then shift; break
else webpaths="$webpaths$arg|"; shift; fi
done
webpaths="${webpaths%?}).*"
local line1="RewriteEngine On"
local line2="RewriteCond %{HTTP_HOST}@@%{HTTP_REFERER} !^([^@]*)@@https?://\1/$webpaths"
local line3="RewriteRule .* - [F]"
echo "generated file: "; echo
echo " $line1"; echo " $line2"; echo " $line3"; echo
[ "$#" -eq 0 ] && args=(".") || args=("$@"); # if no more arrays make an array with './'
for arg in "$args"; do
if [ ! -d "$arg" ]; then echo "Directory '$arg' not found! Skipping";
else
local dir=$(cd $arg; pwd)
local filepath="$dir/.htaccess"
if [ -f "$filepath" ]; then action='OVERWRITE'; else action='Create'; fi
read -p "$action '$filepath' ? y|N " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "$line1" > "$filepath";
echo "$line2" >> "$filepath";
echo "$line3" >> "$filepath";
fi
fi
done
}
# Create standard .htaccess for WordPress at root of installation.
# If an argument (relative or absolute path to a directory) is provided,
# an installation containing that path (somewhere above) will be searched for.
# Otherwise, the current working directory is the search starting point.
# A warning is issued before overwriting .htaccess if it's found.
htaccess_wp_home() {
if [ "$#" -ne 0 ]; then
if [ -d "$1" ]; then cd "$1"
else echo "Directory '$1' not found! Exiting"; return ; fi
fi
while [ ! -d "wp-admin" ]; do
cd .. ;
if [[ "$(pwd)" == '/' ]]; then echo "WordPress not found! Exiting"; return ; fi
done
local filepath="$(pwd)"/.htaccess
if [ -f "$filepath" ]; then action='OVERWRITE'; else action='Create'; fi
read -p "$action '$filepath' ? y|N " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "# BEGIN WordPress" > "$filepath"
echo "<IfModule mod_rewrite.c>" >> "$filepath"
echo "RewriteEngine On" >> "$filepath"
echo "RewriteBase /" >> "$filepath"
echo "RewriteRule ^index\.php$ - [L]" >> "$filepath"
echo "RewriteCond %{REQUEST_FILENAME} !-f" >> "$filepath"
echo "RewriteCond %{REQUEST_FILENAME} !-d" >> "$filepath"
echo "RewriteRule . /index.php [L]" >> "$filepath"
echo "</IfModule>" >> "$filepath"
echo "# END WordPress" >> "$filepath"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment