Tag Archives: htaccess

Cloudflare – ban country with .htaccess

One of my clients asked me to BAN an specific country to one of his sites.
Since we have Cloudflare, this is how I made it…

On .htaccess I’v simply added the following lines – on the top of .htaccess -.

SetEnvIf CF-IPCountry IN BuzzOff=1
SetEnvIf CF-IPCountry PT BuzzOff=1
Order allow,deny
Allow from all
Deny from env=BuzzOff

But remember!
Domain/subdomain must have Cloudflare active on DNS settings…

How to generate a .htpasswd password with PHP?!

This worked for me.

$new_password = password_hash($old_password, PASSWORD_BCRYPT);

password_hash() creates a new password hash using a strong one-way hashing algorithm. password_hash() is compatible with crypt().
Therefore, password hashes created by crypt() can be used with password_hash().

PASSWORD_BCRYPT – Use the CRYPT_BLOWFISH algorithm to create the hash.
This will produce a standard crypt() compatible hash using the “$2y$” identifier. The result will always be a 60 character string, or FALSE on failure. Supported Options:

http://php.net/manual/en/function.password-hash.php

Weird 404 and .htaccess not working properly – cpanel

Took a HUGE time to figure out why my .htaccess rules weren’t working on a CPanel environment.

Like I said on the previous post, CPanel requires the right file permissions on folders and files.

[Tue Jan 06 12:19:40 2015] [crit] [client 84.XX.XX.XXX] (13)Permission denied: /home/asiandee/subdomains/www.domain.com/folder/.htaccess pcfg_openfile: unable to check htaccess file, ensure it is readable

folder has to be with 755
and .htaccess has to be with 644

 

More readings

 

 

 

PHP – 500 internal server error – cPanel

I’v been struggling for hours to solve a weird problem with PHP in a cPanel environment.

This is how my log (/usr/local/apache/logs/error_log) looks like

[Tue Dec 30 21:13:09 2014] [error] [client 88.94.5.xxx] SoftException in Application.cpp:601: Directory "/home/dee/subdomains/www2.domain.com/members" is writeable by group
[Tue Dec 30 21:13:09 2014] [error] [client 88.94.5.XXX] Premature end of script headers: index.php
[Tue Dec 30 21:13:09 2014] [error] [client 88.94.5.XXX] File does not exist: /home/dee/subdomains/www2.domain.com/500.shtml

I thought that was some bad configuration on .htaccess … but that was wrong!
On some environments we need to have the right file/directory permissions so it work…

  • files: 644
  • directories: 755

Continue reading PHP – 500 internal server error – cPanel

Force download MP4 file – PHP or .htaccess

The PHP way – see below this one for a better way

In a client work, I need to force download a MP4 file on some special occasions.
PHP was the solution – in my case -.

rock ssd

This is the small script that I’v placed in download.php – the important stuff -.
Before I have a file_exist condition and other stuffs… but this is the RAW material!

 header ("Content-type: octet/stream");
 header ("Content-disposition: attachment; filename=".$filename.".mp4;");
 header("Content-Length: ".filesize($myFile));
 ob_clean();
 readfile($myFile);

Some of the MP4 files were beeing downloaded! Other weren’t. sh*t!
The problem was a PHP Fatal error!

[03-May-2014 08:09:18] PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 429363201 bytes) in /web/sites/user/domain.com/download.php on line 30

Well!, there are 3 ways to solve this.

  • Edit php.ini and increase the max memory size.
  • Add the following line to the PHP file…
ini_set('memory_limit','128M');
  • Or add the following line to .htaccess
php_value memory_limit 128M

First solution is the best option, since the other 2 might not work due to security settings.

The PHP way *ultimate solution*

This solution we don’t need to increase PHP memory limit.
This will read 8kb of file and push it to the client AND not the full file!

header ("Content-type: octet/stream");
header ("Content-disposition: attachment; filename=".$filename.".mp4;");
header("Content-Length: ".filesize($myFile));
ob_clean();
$handle=fopen($_REQUEST['file'], 'rb');
while (!feof($handle))
{ 
    echo fread($handle, 8192);
    flush();
}
fclose($handle);

The .htaccess way

The normal file type for MP4 is video/mp4.
This way the browser will try to play it and won’t download it.

We can force .htaccess to change it and force the download.

Lets say that we want to force the download when the URL has a ?dl.

RewriteCond %{QUERY_STRING} dl
RewriteRule .*\.mp4 - [T=application/octet-stream]

This way .htaccess www.domain.com/video.mp4?dl and change the file type to application/octet-stream – the one that forces the download.

 

Codeigniter – remove index.php

Bootstrap & Apache! you can see the .htaccess

nginx
Add the following lines on your /etc/nginx/sites-enable/domain.com or default.

# removes trailing "index" from all controllers
 if ($request_uri ~* index/?$)
 {
 rewrite ^/(.*)/index/?$ /$1 permanent;
 }

More details how to set up a nginx environment for CI.
http://wiki.nginx.org/Codeigniter

Apache

Create a .htaccess file on the root of your CI installation (in the same directory where you have system, application, etc).

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

 

Awesome tutorial about mod-rewrite / pretty links
http://stackoverflow.com/questions/20563772/reference-mod-rewrite-url-rewriting-and-pretty-links-explained

 

Thats kinda it!
Basically is a pretty url rewrite condition, for nginx must be also simple as that.
Simple as that!