Regular expressions

Some handy regular expressions terms

If you want to remove only specific tags from the html, you can use following regular expression. I use it along with PHP storm.

For example here is my sample code

    <a href="/samplelink/5656/565">
        <i class="fa fa-trash-o fa-2x" aria-hidden="true"></i>
    </a>
    <a href="/samplelink/21112/21">
        <i class="fa fa-trash-o fa-2x" aria-hidden="true"></i>
    </a>
    <a href="/samplelink/99/997"> 
        <i class="fa fa-trash-o fa-2x" aria-hidden="true"></i>
    </a>

if you want to replace all a tags

<a href="/samplelink/99/997">

I will use the following code in regular expressions to find

<a href=".*?"\s*[/>]* 

Calling controller from a View in Yii

There are two ways to call a controller from a view.

$this->forward('/enggdiary/optimiseroutebygoogleapi',true, array('engineer_id'=>$isAdmin));?>

OR

 

if(($ca=Yii::app()->createController('enggdiary'))!==null){
 list($controller,$actionID)=$ca;
 $controller->init();
 $controller->renderPartial('optimiseroutebygoogleapi',array(
 'engineer_id'=>$model->engineer_id,
 'route_date_string'=>$route_date_string,
 'route_date_time'=>$route_date_time,
 'route_map_results'=>$route_map_results,

'planroute'=>'0',

));
 }

Redirecting all URLs to .com rather than .biz, .co.uk, .info

I was having this problem with my current site. I have several domains like

ukwhitegoods.biz

ukwhitegoods.co

ukwhitegoods.co.uk

ukwhitegoods.com

ukwhitegoods.eu

ukwhitegoods.info

ukwhitegoods.me.uk

Even the problem was Google was indexing all these pages separately and which is not good from the SEO perspective. Therefore, we

This thing is also called as CANONICAL DOMAIN

I used the following code to force all URLs to go to .co.uk

RewriteBase /
 
RewriteCond %{HTTP_HOST} !^(www\.)?ukwhitegoods\.co\.uk$ [NC] ////This means was anything other than this pattern is fired, follow the rule

///NC means case sensitive URL
RewriteRule ^(.*)$ http://www.ukwhitegoods.co.uk/$1 [R=301,L] ////Its a permanent redirect and LAST RULE

So now all my landing pages goes to ukwhitegoods.co.uk

Extending Root Partition in Cent OS from Home directory

For all newbies, it is a common problem when we quickly run out of space (especially if you are a web developer)

Therefore, this tutorial is for users who have much space in home directory but still runs of out space in root

Help from
https://ask.fedoraproject.org/en/question/55867/how-can-i-move-space-from-one-partition-to-another/

Make sure all your hd is ext4 formatted and you are logged in as Root

Now install the software

yum install lvm2

Firstly check the directory structure of CentOS

lvs

To reduce space from home directory type following:

lvreduce --resizefs --size -700G /dev/centos/home 

To Extend space in root directory type following:

lvextend --resizefs --size +700G /dev/centos/root 

Installing CentOS7 on Raid 1 Proliant microserver Gen8

This blog is for installing the CentOS 7 on HP Proliant Microserver Gen8. It can work on other raid devices too.

The tutorial covers the manual partition as some servers are not able to find the boot devices.

Please proceed to following steps.

First of all Go in the

Step1: Go into Bios enter BIOS (press F9 during boot)
– press CTRL+A (Service Option is hidden by default)
– select “Service Options” -> Processor Power and Utilization Monitoring -> Disable

 

please see http://www.serveradminblog.com/2015/04/firmware-bug-the-bios-has-corrupted-hw-pmu-resources/ for details

Also setup System Options -> SATA Controller Options -> Embedded SATA Configuration and select RAID configuration

 

Step2: Go to system Storage configuration by pressing F5 and in it create a RAID 1 logical drive using both the harddisks

Step 3: Now boot system and insert your centos dvd

Step4: Select Language and the version you want to install

Step5: Go in select disk section and select both the disk. At the bottom select manual partition

Step 6: Select New mount point as LVM and click on +

Step 7: Mount point is Boot and desired capacity as 512MB (make sure it is 512 MB on main screen too)

Step 8 Device Type as RAID, File System: ext4, Raid Level is Raid1

Step 9: click on update settings

Step 10: Again click on + from bottom and select new Mount point as ‘/’. Add space how much you want to allot leaving atleast 7GB. I am adding desired capacity as 215GB

Step 11: Now here Device Type is LVM, filesystem as ext4 and now click on Modify for Volume Group

Step 12: Select Raid level in this as RAID1, size policy as Fixed, name as shavg1 and save and then click on update settings

Step 13: Again click on + and now Mount point as SWAP and desired capacity as 6GB

Step 14: Select filesystem as SWAP, device type as LVM and then click on volume group and Modify

Step 15 : Select size policy as Fixed, Raid Level as RAID1 and click save.

Step 16: Check all the values again and click on done

Step 17: There will be summary of changes with top 2 as Destroy format in Red on sdb

Step 18: Accept changes and Click on Begin installation

Step 19: This will take its own time, By the time you can create your root password and create user

Step 20: Reboot the system and it should be good to go.

 

 

 

 

 

.htacess redirect and rewrites

I am writing this blog for a very simple usage of htaccess for redirects and rewriting the url

The first things that needs to be understand for the htaccess REWRITE and REDIRECT modules are the synstax

 

marks the start of line. It means anything that starts with

$ marks the end of line.

(.*) means any character within the url. This gets replaced by $1 at first occurence and $2 as second occurence

L means last code and do not execute anything after this.

R means redirect the page, so the url will be changed. However if you do not add that, it will be same.

 

 

Now here are some sample code

 

RewriteRule ^styles/ - [L,NC]

Now the above code means that if any url starts with style, just let it go through it. for example

http://www.google.com/styles

 

RewriteRule ^resources/image/thumb/(.*)/?$ /download/file.php?id=$1&t=1 [NC,L] # Handle product requests

If any url starts with resources/image/thumb/(.*){THIS COULD BE ANY STRING}/

http://www.google.com/resources/image/thumb/65/

http://www.google.com/resources/image/thumb/was5/

http://www.google.com/resources/image/thumb/adhg5/

Go to looking for

http://www.google.com/download/file.php?id=$1&t=1

Here $1 is (.*){THIS COULD BE ANY STRING}/

http://www.google.com/download/file.php?id=was5&t=1

http://www.google.com/download/file.php?id=65&t=1

 

 

RewriteRule ^resources/image/(.*)/rokbox.jpg?$ /download/file.php?id=$1&mode=view/rokbox.jpg [NC,L] # Handle product requests

RewriteRule ^(.*)/$ $1.html [R,NC,L] # Permanent Move
RewriteRule ^(.*)/(.*)$ $2 [R,NC,L] # Permanent Move

 

 

To exclude a file from htaccess rule: follow this

RewriteCond %{REQUEST_URI} !^/google89b2a81fd1609a6e\.html$

Magento Order email not sent but cron job running | exception ‘Zend_Mail_Transport_Exception’ with message ‘Missing To header’ in /var/www/html/shop/lib/Zend/Mail/Transport/Sendmail.php:182

Solution 1

Are you getting following message in your magento shop /var/exception.log

2015-10-28T00:05:02+00:00 ERR (3):
exception 'Zend_Mail_Transport_Exception' with message 'Missing To header' in /var/www/html/shop/lib/Zend/Mail/Transport/Sendmail.php:182
Stack trace:
#0 /var/www/html/shop/lib/Zend/Mail/Transport/Abstract.php(337): Zend_Mail_Transport_Sendmail->_prepareHeaders(Array)
#1 /var/www/html/shop/lib/Zend/Mail.php(1194): Zend_Mail_Transport_Abstract->send(Object(Zend_Mail))
#2 /var/www/html/shop/app/code/core/Mage/Core/Model/Email/Queue.php(236): Zend_Mail->send()
#3 [internal function]: Mage_Core_Model_Email_Queue->send(Object(Mage_Cron_Model_Schedule))
#4 /var/www/html/shop/app/code/core/Mage/Cron/Model/Observer.php(326): call_user_func_array(Array, Array)
#5 /var/www/html/shop/app/code/core/Mage/Cron/Model/Observer.php(72): Mage_Cron_Model_Observer->_processJob(Object(Mage_Cron_Model_Schedule), Object(Mage_Core_Model_Config_Element))
#6 /var/www/html/shop/app/code/core/Mage/Core/Model/App.php(1357): Mage_Cron_Model_Observer->dispatch(Object(Varien_Event_Observer))
#7 /var/www/html/shop/app/code/core/Mage/Core/Model/App.php(1336): Mage_Core_Model_App->_callObserverMethod(Object(Mage_Cron_Model_Observer), 'dispatch', Object(Varien_Event_Observer))
#8 /var/www/html/shop/app/Mage.php(448): Mage_Core_Model_App->dispatchEvent('default', Array)
#9 /var/www/html/shop/cron.php(77): Mage::dispatchEvent('default')
#10 {main}
As you might understand that the problem is due to sender missing. But, in Magento 1.9, this is serious problem as it might stop all your order emails to stop sending. The solution is finding the order with the null email address and deleting the email_queue cron job of that order. Here are the tricks

First find the order that have no email with following command


 SELECT *
 FROM `sales_flat_order`
 WHERE `customer_email` IS NULL
 

Now I recommend to update that order

UPDATE `sales_flat_order` SET `customer_email` = '*******@yahoo.com' WHERE `sales_flat_order`.`entity_id` =YOUR_ORDER_ID ;

Now find this order in core_email_queue


select * from `core_email_queue` WHERE entity_id='YOUR_ORDER_ID'; ///Note this is not order number. 
////you can get order id from the url of admin of order

There should be 2 entries if you send yourself as well order copy

And now find the corresponding in core_email_queue_recipients


select * from `core_email_queue_recipients` WHERE message_id='MESSAGE_ID_FROM_core_email_queue'
select * from `core_email_queue_recipients` WHERE message_id='MESSAGE_ID_FROM_core_email_queue'

If you do not have message_id in core_email_queue_recipients, then you should delete it from core_email_queue

 


DELETE FROM core_email_queue WHERE message_id='MESSAGE_ID_MISSING_FROM_core_email_queue'

Solution 2: Quick & Dirty one

Other quick and dirty solution which I have not tried, but you can try on test server first is to


TRUNCATE core_email_queue_recipients;
TRUNCATE core_email_queue;

This should clear all your email queues but will delete the working email queues too.

Magento Cron jobs not running and no error: Flush Storage Cache

This is from post

http://blog.husseycoding.co.uk/2012/04/magento-cron-jobs-not-running.html

Sometimes you can find the cron jobs for your Magento store just aren’t running, or are running intermittently even though everything has been setup correctly and there is apparently no reason for it.

Surprisingly you might find the problem is actually down to a simple caching issue with a one line code fix. Magento caches it’s cron schedule and reads this cached content on subsequent runs of cron rather than recreating the schedule each time to determine which jobs it should execute. The problem is sometimes jobs just don’t get cached despite them being configured correctly so when the time comes for them to execute nothing actually happens.

It’s worth noting that the cron schedule is cached even if all caching is disabled in admin, and the only way to clear the cached schedule through admin is to Flush Cache Storage under System->Cache Management. Unfortunately this doesn’t then cause the jobs to be correctly cached the next time cron is run.

So the fix is to simply disable the caching of the cron schedule, and don’t worry, benchmarks with caching disabled shows this to have no negative impact on site performance.

Copy:
app/code/core/Mage/Cron/Model/Observer.php
to:
app/code/local/Mage/Cron/Model/Observer.php
if it’s not already there. Open the copied file and find the following line inside the generate() method:
Mage::app()->saveCache(time(), self::CACHE_KEY_LAST_SCHEDULE_GENERATE_AT, array(‘crontab’), null);
This is the line that initiates caching of the cron schedule, so just comment it out:
//Mage::app()->saveCache(time(), self::CACHE_KEY_LAST_SCHEDULE_GENERATE_AT, array(‘crontab’), null);
Save the changes and Flush Cache Storage to clear any currently cached cron jobs and your store should now always execute properly configured cron jobs.