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 ^(.+[^/])/$ http://%{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

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

USE INFORMATION_SCHEMA;
SELECT 
    TABLES.table_name
FROM TABLES
LEFT JOIN KEY_COLUMN_USAGE AS c 
ON (
       TABLES.TABLE_NAME = c.TABLE_NAME
   AND c.CONSTRAINT_SCHEMA = TABLES.TABLE_SCHEMA
   AND c.constraint_name = 'PRIMARY'
)
WHERE 
    TABLES.table_schema <> 'information_schema'
AND TABLES.table_schema <> 'performance_schema'
AND TABLES.table_schema <> 'mysql'
AND c.constraint_name IS NULL;

A friend advise: “The result list of this query should be 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)