Tutorials for Magento and Joomla! - Yireo

Prevent direct access to Magento using htaccess

Duplicate content is regarded by many bad for search engine optimization. Though it could be argued whether this is needed or not, you might be tempted to make the Magento webshop only accessible through Joomla! and not any more by accessing the Magento application directly. While MageBridge translates every single Magento-link to a MageBridge link, visitors can still visit the Magento application if they would know the direct URL. Because MageBridge serves the same content as Magento does, this is duplicate content. And if visitors can access Magento directly, there is a (very small) chance search engines will find out about this as well.

mb_graph_simple

The best strategy would be to redirect every visitor from Magento to Joomla!. But if somebody visits a specific URL in Magento, it would be unfriendly to redirect that user to the default Joomla! homepage - you want to redirect the user directly to the same page within Joomla!. Lucky enough, converting the Magento URL into a Joomla! URL is easy. MageBridge uses the Magento URL-system, so a rewrite of the old Magento page to the new Joomla! page is very easy.

https://MAGENTO/nine-west-women-s-lucero-pump.html
https://JOOMLA/shop/nine-west-women-s-lucero-pump.html

Editing .htaccess

Note that this strategy involves hacking of the .htaccess file of Magento. Before you start off changing things, make a good backup of the current .htaccess file. Also note that editing RewriteRules requires a solid understanding of how Apache works and how RewriteRules work. The example below is merely given as a suggestion, not as a working file for all environments.

The strategy involves blocking visitors from accessing the main index.php file, while the MageBridge entry-point (magebridge.php) is still accessible. Open up the .htaccess file and locate the section beginning with <IfModule mod_rewrite.c>.

The main change is to replace the redirect to index.php with a redirect to your Joomla! site.

<IfModule mod_rewrite.c>
# enable rewrites
Options +FollowSymLinks
RewriteEngine on

# you can put here your magento root folder
# path relative to web root
#RewriteBase /magento/

## workaround for HTTP authorization in CGI environment
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# always send 404 on missing files in these folders
RewriteCond %{REQUEST_URI} !^/(media|skin|js)/

# rewrite non-existing files/directories that are not the admin
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !^/admin/
RewriteCond %{REQUEST_URI} !^/admin$
RewriteRule ^(.*)$ http://JOOMLA/shop/$1 [R=301,L]

# rewrite requests for the default homepage
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ http://JOOMLA/shop/ [R=301,L]

# rewrite requests for /index.php/ pages except for admin-pages
RewriteCond %{THE_REQUEST} /index.php
RewriteCond %{REQUEST_URI} !^/admin/
RewriteCond %{REQUEST_URI} !^/admin$
RewriteCond %{REQUEST_URI} !^/magebridge/
RewriteCond %{REQUEST_URI} !^/js
RewriteCond %{REQUEST_URI} !^/skin
RewriteCond %{REQUEST_URI} !^/media
RewriteRule ^(.*)$ http://JOOMLA/shop/ [R=301,L]

# rewrite everything else to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]

</IfModule>

Note that we use SEF URLs here - we have enabled SEF from with the Joomla! Global Configuration. The page https://JOOMLA/shop/ is served by a MageBridge Root Menu-Item (configured from with the Joomla! Menu Manager) with an Alias shop. Now with this change, if a visitor would visit the URL https://MAGENTO/nine-west-women-s-lucero-pump.html, he/she will be redirected immediately to the URL https://JOOMLA/shop/nine-west-women-s-lucero-pump.html.

Sample htaccess

Here is a downloadable example of a htaccess-file based on the rules above. Note that you might need to modify various details.

Different admin-directory

If you are using a different backend URL - for instance secret instead of admin - then you need to modify the following lines:

RewriteCond %{REQUEST_URI} !^/secret/
RewriteCond %{REQUEST_URI} !^/secret$

Magento in a sub-directory

Please note that when using Magento in a sub-directory you need to make some changes in these rules. First of all, Magento requires you to modify the line with RewriteBase - you need to activate that line by removing the # and by modifying the folder-path to your actual Magento-folder.

RewriteBase /magento/

Also, every occurance of a ^/ needs to be replaced with your Magento-folder. For instance:

RewriteCond %{REQUEST_URI} !^/magento/skin
RewriteCond %{REQUEST_URI} !^/magento/magebridge

Example for a sub-directory "magento"

A complete example is here:

RewriteBase /magento/
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

RewriteCond %{REQUEST_URI} !^/magento/(media|skin|js)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !^/magento/magebridge/
RewriteCond %{REQUEST_URI} !^/magento/admin/
RewriteCond %{REQUEST_URI} !^/magento/admin$
RewriteCond %{REQUEST_URI} !^/magento/js
RewriteCond %{REQUEST_URI} !^/magento/skin
RewriteCond %{REQUEST_URI} !^/magento/media
RewriteRule ^(.*)$ http://JOOMLA/shop/$1 [R=301,L]

RewriteCond %{REQUEST_URI} ^/magento/$
RewriteRule ^(.*)$ http://JOOMLA/shop/ [R=301,L]

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

Use a robots.txt

Instead of working with difficult htaccess-rules that block access to Magento, you might just as well add a robots.txt file to the Magento root, that tells search engines to skip this entire site for indexing. It could be a much simpler solution.

The robots.txt file would then contain the following:

User-Agent: *
Disallow: / 
Created on Tuesday, 12 January 2010
Modified on Tuesday, 17 April 2012

About Yireo

Yireo tries to help webdevelopers build successful Joomla! and Magento sites.

More about Yireo