Exclude directories from URL rewriting

Posted in SEO on December 3rd, 2009 by Ilir Fekaj – Be the first to comment

Rewriting your URLs as directories in .htaccess is a really cool feature which greatly improves usability and search engine visibility of your pages. However, this can make real directories inaccessible and you may wish to exclude these from being rewritten. For example, you might want to exclude your temp, stats or admin directory. The simplest way to do this would be to place the following code in your .htaccess file:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L]

The first line of code checks if the directory requested by user exists on the server file system, and if it does exist, it doesn’t serve index.php. Second line does the same thing for files. In case request is not made for any files or directories that exist, it performs rewrite on index.php.
This works in many cases, but if you have more complex rewrites performed on several files, I found that this code does better job:

RewriteRule ^(temp|stats|admin)(/.*)?$ - [L]

Put all directories you want to exclude from rewrite in brackets and separate them with pipe symbol “|”.

Post to Twitter Tweet This Post

Frame busting code and AdSense

Posted in Javascript on November 19th, 2009 by Ilir Fekaj – 1 Comment

If you are tired of your site being framed by other sites or image search engines, you would certainly have thought of using some of Javascript code to kill unwanted frame. However, there is concern that this code may cause double page impression of your AdSense or other ads. To test this, I created a small experiment by adding Javascript alert near the end of my page so it runs after frame killer/redirect occurs. To my surprise, Chrome, Opera and Safari loaded two alert boxes, which effectively proved that all Javascript (including ads) is fired up twice. I solved this problem (hopefully) by stopping execution of all scripts after redirection. Here is my final code:

if (top.location != self.location) {
top.location.replace(self.location);
if(navigator.appName == "Microsoft Internet Explorer"){
window.document.execCommand('Stop');
}
else{
window.stop();
}
}

Please let me know what you think.

Post to Twitter Tweet This Post

Prevent specific sites from hotlinking using .htaccess

Posted in SEO on October 10th, 2009 by Ilir Fekaj – Be the first to comment

When I first created AvatarsDB site, my idea was to allow people to simply link to individual avatar images without need to search for image hosts or download images to their computers. However, after some time there were people who started to abuse this service. I had cases when people built entire sites and blogs just by linking to thousands of images from my site. No need to mention that this wasted gigabytes of my bandwidth and sent hundreds of thousands of useless request to my server without providing even single link back. I wanted some solution which will allow legitimate users to use this service and block bandwidth leeching.

Solution is very simple and it involves editing your .htaccess file and creating replacement image which you will show to visitors of offending site. This can also bring some traffic to you if you put name or address of your site. Simply put this code in directory where you keep your images or in root if you wish to prevent hotlinking in all directories:

RewriteEngine on
RewriteCond %{HTTP_REFERER} ^http://(.+\.)?evilhotlinker\.net/ [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://(.+\.)?leecher\.com/ [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://(.+\.)?annoyance\.net/ [NC]
RewriteRule .*\.(jpg|gif|bmp|png)$ images/nohotlink.gif [L]

You can put as many sites in this list as you like, just make sure lines before last one on the list end with [NC,OR] which tells Apache to continue matching. If you want to save bandwidth, you can also send error 403 Forbidden by editing last line to this:

RewriteRule .*\.(jpg|gif|bmp|png)$ - [F]

You can also block all hotlinking and allow only your site:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://yourdomain.com.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.yourdomain.com.*$ [NC]
RewriteRule .*\.(jpg|gif|bmp|png)$ images/nohotlink.gif [L]

You should put all these codes before you do any URL rewriting or they might not work. When you’re done editing, make sure you upload it in text mode or it also may not work.

Post to Twitter Tweet This Post

Calculate image aspect ratio in PHP

Posted in PHP on September 13th, 2009 by Ilir Fekaj – 1 Comment

Calculating image aspect ratio in PHP is very simple. First you need to find greatest common divisor(GCD). You can do that by using following function:

function GCD($a, $b) {
while ($b != 0)
$remainder = $a % $b;
$a = $b;
$b = $remainder;
}
return abs ($a);
}

If you compiled your PHP version with GMP functions, you can also use following:

$gcd = gmp_gcd($a, $b);

After finding GCD, simply divide both sides with it:

$a = 2048; // screen width
$b = 1152; // screen height
$gcd = GCD($a, $b);
$a = $a/$gcd;
$b = $b/$gcd;
$ratio = $a . ":" . $b;

Post to Twitter Tweet This Post