Cookie Consent by Free Privacy Policy Generator Page 5 of 7 for Home | Igor Moiseev

Igor Moiseev Applied mathematician, Web Developer

Allow optional trailing slash in Apache with Rewrite module

Apache’s handy mod_rewrite module is helping to correct this unfortunate human typos.

At first let’s check whether the Apache’s mod_rewrite is enabled.

# a2enmod rewrite
Module rewrite already enabled

If it was not, then reload configuration

# service apache2 reload
 * Reloading web server config apache2                      [OK]

to assure that the rewrite will work you need to assure the one more thing, it is AllowOverride option in the VirtualHost configuration

<VirtualHost *:80>

...

<Directory /var/www/example.com/htdocs>
        Options FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
</Directory>

....

</VirtualHost>

Now put the following snippet into .htaccess located in the folder of your website (/var/www/example.com/htdocs)

RewriteEngine On

# remove trailing slash
RewriteCond %{HTTPS} off
RewriteRule ^(.+[^/])/$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteRule ^(.+[^/])/$ https://%{HTTP_HOST}/$1 [R=301,L]

That’s it, happy re-writing!

Encode string to HTML entities via jQuery

The follwoing will encode your string to HTML entities

jQuery('<div />').text('Some text with <div>html</div>').html()

and the output will look like

"Some text with &lt;div&gt;html&lt;/div&gt;"

To decode we just switch methods

jQuery('<div />').html('Some text with &lt;div&gt;html&lt;/div&gt;').text()

produces

"Some text with <div>html</div>"

The jQuery magic!

Subversion commands

Delete recursively the .svn directories

$ rm -rf `find . -type d -name .svn`

Find files not under the version control

$ svn status | grep -e ^?

How to remove all deleted files from repository

$ svn st | grep '^!' | awk '{print $2}' | xargs svn delete --force

grep on folder with excluding of .svn dirs

$ grep -r 'content_graphic' assets/js --exclude=*\.svn*

Find all tables without primary key in MySQL

Search across all databases (schemas) for tables without primary key

The following query obtains the list of tables without primary key, those who destroys the database performance

SELECT 
    t.TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES AS t
LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS k
ON (
       t.TABLE_NAME = k.TABLE_NAME
   AND k.CONSTRAINT_SCHEMA = t.TABLE_SCHEMA
   AND k.constraint_name = 'PRIMARY'
)
WHERE 
    t.TABLE_SCHEMA NOT IN ('information_schema', 'performance_schema', 'mysql', 'sys')
AND k.constraint_name IS NULL;

In this example, the INFORMATION_SCHEMA.TABLES table is used to find all t. The TABLE_NAME column is selected, and the WHERE clause is used to filter system related databases.

LEFT JOIN is used to join with table KEY_COLUMN_USAGE and filter the tables that do not have a primary key.

Restrict search for tables without primary key to a specific databases (schema)

SELECT 
    t.TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES AS t
LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS k
ON (
       t.TABLE_NAME = k.TABLE_NAME
   AND k.CONSTRAINT_SCHEMA = t.TABLE_SCHEMA
   AND k.constraint_name = 'PRIMARY'
)
WHERE 
    t.TABLE_SCHEMA NOT IN ('information_schema', 'performance_schema', 'mysql', 'sys')
AND t.TABLE_SCHEMA = '<database name>' -- put database name here
AND k.constraint_name IS NULL;

A friendly advise, the result list of these queries should be an Empty set.

Happy querying!

The rmagic gem trouble on linux

Hope this tip will save to someone time for fun coding, if you suddenly get this error while installing the RMagick gem on linux

$ sudo gem install rmagick
Building native extensions.  This could take a while...
ERROR:  Error installing rmagick:
    ERROR: Failed to build gem native extension.
Can't install RMagick 2.13.4. Can't find Magick-config

That means the devel package is not installed, so you pick it up on Ubuntu > 12.04 with this command

$ sudo apt-get install libmagick++-dev

and voilà

$ sudo gem install rmagick
Building native extensions.  This could take a while...
Please report any bugs. See https://github.com/gemhome/rmagick/compare/RMagick_2-13-2...master and https://github.com/rmagick/rmagick/issues/18
Successfully installed rmagick-2.13.4
Parsing documentation for rmagick-2.13.4
Installing ri documentation for rmagick-2.13.4
Done installing documentation for rmagick after 7 seconds
1 gem installed

checkout if everything is OK

$ dpkg -l | grep imagemagick
ii  imagemagick          8:6.7.7.10+dfsg-4ubuntu1   amd64        image manipulation programs
ii  imagemagick-common   8:6.7.7.10+dfsg-4ubuntu1   all          image manipulation programs -- infrastructure

$ gem list | grep rmagick
rmagick (2.13.4)

moiseevigor.github.io is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means to earn fees when linking to Amazon.com and affiliated sites.