11 January 2013

Installing Magento version 1 in a Subdirectory

I had a situation recently where I wanted to get Magento running under a subdirectory of web root, with Magento requests being seamlessly rewritten from a subdomain to the Magento install, but I didn't want the subdirectory to appear in the URL.

It's easy enough to get Magento running from a subdirectory if you don't mind having that subdirectory appearing in the URL itself directly after the host.  You just copy Magento to the subdirectory, access it from http://domain.com/subdirectory and install as normal.  This means however that your base URL's then include that subdirectory, and all URLs are generated with it.

If you want to have Magento run from a subdirectory without any indication of this being the case (so no directory in the url), you will find that you come across all kinds of wierd and wonderful solutions from huge, $_POST data disabling .htaccess rewrites (meaning you can no longer save in admin, add any items to the cart etc), to the addition of RewriteBase declarations that cripple any other domains you have running from the web root directory, to moving index.php and .htaccess files to web root and making all kinds of changes to index.php to add in the subdirectory.

Well with a bit of time, and some local development environment debugging, the solution actually boils down to 1 rewrite rule in a web root .htaccess file and one line added to index.php.  You don't need to move any Magento files anywhere.

So lets start by installing the store, copy Magento to the subdirectory, and run the installation like normal from http://domain.com/subdirectory or whatever your address might be.  Once installation is complete login to admin and I tend to disable and clear cache to stop potential caching issues confusing results.  You can do this under System -> Cache Management.  Then navigate to System -> Configuration -> Web and remove the subdirectory you will see appended to the Secure and Insecure Base URL's (ensure the Base URL's still finish with a trailing slash).  Save the changes (which will break the store, don't worry).

Now create .htaccess if one isn't already there under web root and add the following into it, replace subdomain.domain.com with your subdomain and domain, and magento with your subdirectory:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$
RewriteCond %{REQUEST_URI} !^/magento/
RewriteRule ^(.*)$ /magento/$1
This is a simple rewrite that just rewrites all requests for the subdomain called subdomain to the magento subdirectory.

NOTE: If you aren't using a subdomain, just alter the HTTP_HOST line to instead be:
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
or:
RewriteCond %{HTTP_HOST} ^domain\.com$
depending on what your web address is.

Next we need to make a change to a $_SERVER variable used to build the request URI by an underlying Zend method before the request is dispatched by the front controller.  The class is Zend_Controller_Request_Http, and about halfway down you will see the methods setBaseUrl() and setRequestUri().  Without going into too much unnecessary detail, these methods end up creating an invalid request string by running a substr() of $_SERVER['REQUEST_URI'] and the string length of $_SERVER['SCRIPT_NAME'].  When not running from a subdirectory this is fine, as it just ends up stripping off index.php from the REQUEST_URI, but when running from a subdirectory, $_SERVER['SCRIPT_NAME'] reflects the actual script location from web root, so whereas normally it would just contain /index.php, now it contains /magento/index.php.  The end result of this is that more than just /index.php gets stripped off the REQUEST_URI, this creates an invalid path, and Magento loads a 404 error page.

All we need to do to correct this is add the following inside <?php ?> tags at the top of index.php within the subdirectory:
$_SERVER['SCRIPT_NAME'] = '/index.php';
And that's it, you should now have a fully functional, seamlessly rewritten Magento store running from a subdirectory.

8 January 2013

Twitter Reader Magento Extension Launched

Hussey Coding is proud to announce the launch of its new Magento extension Twitter Reader.  Integrating seamlessly with the latest Twitter API technology this extension allows you to quickly and easily display tweets on any page and in any position.

Setting up communications with the Twitter API is made straightforward with the extension including simple links and instructions and how to grant your Magento store access to your tweets.  Detailed admin information also tells you the status of communications with the Twitter API and allows you to reset access if you decide you want to alter or create a new Twitter application.

Admin Twitter Configuration
Admin Twitter Configuration

Admin settings allow you to include dates in your tweets, set the number of tweets to display, and configure communications with the Twitter API.  You can also set blocks of tweets to be output to your store in a range of fixed positions, or any position of your choice using the custom option.

Tweets Output Block
Tweets Output Block

The extension is compatible with Magento 1.4.2.0 and above.  You can view a quick video demo of it here, view all Hussey Coding extensions including this one on Magento Connect, and you can purchase Twitter Reader here.

CMS Menu Content Magento Extension Launched

Hussey Coding is proud to announce the launch of its new Magento extension CMS Menu Content.  The extension greatly improves the readability of your CMS pages by adding tab style content switching to any CMS page.

The extension allows the addition of any number of static blocks to a CMS page and displays a menu of links on the page to allow you to switch between the content of the static blocks you have included.  This switching is achieved using JavaScript, ensuring the extension is fully SEO compatible with search engines still having access to the full page content and all included static blocks.

Menu Content Page
Menu Content Page

Building CMS pages in this way allows you to have a high level of content, over just a few pages whilst keeping information quick to find, and highly readable.

CMS Page Admin Settings
CMS Page Admin Settings

CMS page admin settings display a list of all enabled static blocks, allows you to set their display order, and also style the related links that are displayed when viewing the page.


Static block admin settings allow you to set custom text to use for the link appearing on CMS pages also enable or disable the use of that block in CMS pages.

The extension is compatible with Magento 1.4 and above.  You can view a quick video demo of it here, view all Hussey Coding extensions including this one on Magento Connect, and you can purchase CMS Menu Content here.

Custom Order Grid Magento Extension Launched


Hussey Coding is proud to announce the launch of its new Magento extension Custom Order Grid.  The extension is designed to greatly enhance a store owners ability to efficiently find orders by optionally adding many more columns to the sales order grid to sort and filter by.

This extension allows you to fully customise and sort the order of columns displayed in the admin order grid, and give full sorting and filtering capability for all of these columns via a range of new settings in admin under System → Configuration → Hussey Coding → Custom Order Grid.  These include a multi select form element for choosing the columns you want to include in the grid, information about column ordering (set by column selection order), the default sort column, and default sort order.
Order Grid Admin Settings
Order Grid Admin Settings

It is possible to use any of the following columns in the order grid, the order number will always be displayed:
  • Purchased From (Store)
  • Purchased On
  • Order Modified
  • Bill to Name
  • Ship to Name
  • G.T. (Base)
  • G.T. (Purchased)
  • Status
  • SKU
  • Shipping Method
  • Coupon Code
  • Customer Email
  • Shipping (Base)
  • Shipping (Purchased)
  • Subtotal (Base)
  • Subtotal (Purchased)
  • Tax (Base)
  • Tax (Purchased)
  • Guest Checkout
  • Currency
  • Product Count
Customised Order Grid
Customised Order Grid

The extension is compatible with Magento 1.4 and above.  You can view a quick video demo of it here, view all Hussey Coding extensions including this one on Magento Connect, and you can purchase Custom Order Grid here.