Skip to content

Instantly share code, notes, and snippets.

@jvanja
jvanja / .htaccess
Last active June 15, 2020 09:22
Rewrite rule for .htaccess for rewritting /page/subpage to ?page=page&subpage=subpage
# redirects for url params
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-z\-\_]+)$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9-z\-\_]+)/$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9-z\-\_]+)/([a-zA-Z0-9-z\-\_]+)$ index.php?page=$1&subpage=$2
RewriteRule ^([a-zA-Z0-9-z\-\_]+)/([a-zA-Z0-9-z\-\_]+)/$ index.php?page=$1&subpage=$2
# index file priority
DirectoryIndex index.html index.htm index.php welcome.html
@jvanja
jvanja / Drush Cheats
Last active April 2, 2018 08:57
Useful Drush commands
# download stable Drupal
drush dl drupal
# install Drupal site with admin/admin acc
sudo drush site-install standard --account-name=admin --account-pass=admin --db-url=mysql://drupal7:drupal7@localhost/drupal7
# clear drupal cache
drush cc
# download and enable module
@jvanja
jvanja / Regex - in between
Last active December 27, 2015 14:59
Regex for search and replace in between two strings: string 'start' and string 'end', using 'look ahead' and 'look behind'.
(?<=start)(.*)(?=end) // lazy - it will select until LAST occurence
(?<=start)(.*?)(?=end) // not lazy - it will select until FIRST occurence
@jvanja
jvanja / Git undo commit
Created April 12, 2014 22:23
Git undo commit
# undo commit and REVERT the files to the last HEAD
git reset --hard HEAD~1
# undo commit but PRESERVE the files before the last commit
git reset HEAD~1
@jvanja
jvanja / JavaScript: PubSub
Last active August 29, 2015 14:01 — forked from cassiano-gists/gist:4141404
JavaScript: Pub/Sub
// Works in modern browsers + IE9, but Modernizr has a polyfill baked in for function.bind.
// Hat tip Paul Irish
var o = $( {} );
$.subscribe = o.on.bind(o);
$.unsubscribe = o.off.bind(o);
$.publish = o.trigger.bind(o);
// Usage
$(document.body).on( 'click', function() {
@jvanja
jvanja / 755_On_All.sh
Created October 30, 2015 08:45
Unix FIND all files that do not have 755 permision and CHMOD 755 on them
find . -not -perm 755 -type f -exec chmod 755 {} \;
@jvanja
jvanja / Top10.sh
Created October 30, 2015 08:47
Find 10 largest files under current directory and show them
find . -type f -exec ls -s {} \; | sort -n -r | head -10
@jvanja
jvanja / Rename All.sh
Created November 2, 2015 16:16
Find all files of type of HTML and change their names to index.html
find . -name "*.html" -exec bash -c 'cp "{}" "$(dirname "{}")"/index.html' \;
@jvanja
jvanja / awk_examples.sh
Created January 12, 2016 09:28
Useful awk examples
#searches for 'search_term' and prints all columns
awk '/search_term/' file.txt
#searches for 'search_term' and prints columns 1 and 4 separated with tab (the first column is index 1)
awk '/search_term/ {print $1 "\t" $4}' file.txt
#searches for 'search_term' and prints the number of occurances
awk '/search_term/{++cnt} END {print "Count = ", cnt}' file.txt
#print lines having more than 18 characters
@jvanja
jvanja / find_all_pngs_in_images_folders_and_exec-pngquant.sh
Last active September 17, 2021 14:41
Finds all pngs in "*/images" folders and executes pngquant on them overwriting the original files.
find ./ -path '*/images/*.png' -type f -exec pngquant -f --ext .png {} \;