.htaccess Tips & Tricks for Zen Cart

By Melanie Prough on Sunday, November 8, 2009
Filed Under: Zen Cart SEO, Zen Cart Tips, Zen Cart Tutorials












One of the most common questions I answer on the Zen Cart forum is about redirects and rewrites for your htaccess. I am going to give you the normal rules for many Zen Cart htaccess needs in this post…. But before you do anything, make a copy of your .htaccess as a backup.

There are times, due to site and server configurations where the standard rules will not work… These are times when you will have to read a bit and do some trial and error. Important note here… If you have a problem, the results are not as expected.. or you produce a 500 error.. relax and use your backup =-)

What is an htaccess

Simply stated, an htaccess (hypertext access)¬† is a directly level configuration file for Apache servers used to override/add to the server’s global configuration rules.

Canonical Redirect for Zen Cart

This is a very common request to redirect all http request for your Zen cart to the proper canonical url (www or non). Note that these are the rules we use and not the only way to do this. First thing is that you MUST use the same canonical url are your Zen Cart is configured to use (or change it) in includes/configure.php and your_admin/includes/configure.php.

Redirect non www to the www

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
</IfModule>

Redirect www to the non www

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^domain\.com
RewriteRule (.*) http://domain.com/$1 [R=301,L]
</IfModule>

Just a quick note that country code domains such as .co.uk will require some tweaking as they are a subdomain to begin with.

Default indexing is a very solid thing to do for your Zen Cart. While we realize that Google, for example, really handles duplicate content well.. It is best to avoid creating it. So we would add a rules like below. Note that you will need some tweaking here if you have additional folders within your Zen Cart directory structure.

RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php?\ HTTP/
RewriteRule ^(.*)index\.php?$ http://domain.com/$1 [R=301,L]

or for the www

RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php?\ HTTP/
RewriteRule ^(.*)index\.php?$ http://www.domain.com/$1 [R=301,L]

Next, lets say you are changing to Zen Cart from some *other shopping cart platform and wish to retain your link juice, PageRank and such by redirecting the old urls to the new.

We can do this easily by sending all requests to the root, but it’s far more effective to use some logical redirecting to the proper area in the *new cart. For this a query string redirect is perfect.

RewriteEngine On
RewriteCond %{QUERY_STRING} category=76
RewriteRule (.*) http://www.domain.com/index.php?main_page=index&cPath=220$1? [R=301,L]

In this case, we identified a string (category=76) in all urls of our old site’s category and are redirecting them by matching the string to our new category. Remember to use your proper canonical.

How about you move to Zen Cart from an ASP cart and wish to redirect all ASP urls? Remember to use your proper canonical url.

RewriteEngine On
RewriteCond %{HTTP_HOST} (www.)?domain.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.asp(.*)$ http://domain.com/? [R=301,L]

Redirect all .htm or .html pages to the root?

RewriteEngine On
RewriteCond %{HTTP_HOST} (www.)?domain.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.htm(.*)$ http://www.domain.com/? [R=301,L]

RewriteEngine On
RewriteCond %{HTTP_HOST} (www.)?domain.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html(.*)$ http://www.domain.com/? [R=301,L]

How about you moved your Zen Cart from a directory to the root?

The absolute easiest and most effective way to do this is to redirect these urls to their exact match in the root. This file would go in your root (public_html). Remember to use your proper canonical.

RewriteEngine On
RedirectMatch ^/folder/.* http://www.domain.com/

Need to redirect just one page?

Redirect 301 /file.html http://www.domain.com/

We have seen vulnerabilities for inserted hack files to change files permissions, run queries and execute other nasty scripts. So you can create an .htaccess for folders like your images to prevent the execution of scripts within these ancillary folders.

Options -ExecCGI

Prevent other sites from image  hot linking and stealing bandwidth from you and instead send them to your main page.

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain.com/.*$ [NC]
RewriteRule \.(gif|jpg|swf|flv|png)$ http://www.domain.com/ [R=302,L]

Be careful with this one, if you are using absolute links (http://domain.com/images/.logo.gif) instead of images/logo.gif you will also redirect yourself =P

Note that if you are using an SEO urls or other rewrite module you will be doing that tweaking we mentioned earlier… as they are very likely to cause conflicts.

Tags: , , , , , , , , , , , , , , , , , , ,

Comments are closed.

Template Original