Adding Additional Rewrite Rules to Joomla .htaccess

Written by Marcus Miller on . Posted in Archive

Note: this is an old post and relates to Joomla 1.5. 

If you are moving from a static website to Joomla! or simply upgrading or changing the structure of your site then you may find yourself in a position where you need to add some custom mod_rewrite rules to your Joomla htaccess file.

Rewriting without Joomla SEF URLs

If you are not using search engine friendly URLs then you will need to first create a .httaccess file in the root of your web directory. You can then initialise mod_rewrite, set your rewrite base and add your rewrite rules.

Your .htaccess file will look something like this:

# enable mod rewrite and set url base
RewriteEngine On
RewriteBase /

# rewrite all variations of domain to www.
RewriteCond %{HTTP_HOST} ^bowlerhatsolutions.co.uk [NC]
RewriteRule ^(.*)$ http://www.bowlerhatsolutions.co.uk/$1 [L,R=301]

# basic rewrite rules old sitemap page to homepage
rewriterule ^sitemap\.html$ / [R=301,L]

# basic rewrite rule old web-site-design.html page to new web-design.html
rewriterule ^web-site-design\.html$ /web-design.html [R=301,L]

Joomla SEF URLs vs Mod Rewrite

We won’t go into all the details as there are much better guides out there but what we really want to cover is adding rewrite rules to your installation when you are using the Joomla Search Engine Friendly URLs (SEF) with htaccess enabled. This is the trickier option as you have to ensure that you don’t upset joomla mod rewrite rules already in the htaccess file.

If you have enabled SEF & .htaccess in the Joomla Global Configuration then you will already have a fairly intimidating .htaccess file in your Joomla root directory. To add our rules we have to make changes to this file and not tip up the apple cart and break the existing URLs.

1. Adding New Rules

Your .htaccess will contain a chunk of rules to block out some common exploits that is clearly shown with a opening and closing comment and directly after that block, again commented, are the core SEF rules.

You want to add your new rules before the core SEF rules and the resulting htaccess may look something like this:

## Can be commented out if causes errors, see notes above.
 #Options +FollowSymLinks
# mod_rewrite in use
RewriteEngine On
########## Begin - Rewrite rules to block out some common exploits
## If you experience problems on your site block out the operations listed below

## This attempts to block the most common type of exploit `attempts` to Joomla!
#
# Block out any script trying to set a mosConfig value through the URL
 RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR]
 # Block out any script trying to base64_encode crap to send via URL
 RewriteCond %{QUERY_STRING} base64_encode.*\(.*\) [OR]
 # Block out any script that includes a tag in URL
 RewriteCond %{QUERY_STRING} (\|%3E) [NC,OR]
 # Block out any script trying to set a PHP GLOBALS variable via URL
 RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
 # Block out any script trying to modify a _REQUEST variable via URL
 RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
 # Send all blocked request to homepage with 403 Forbidden error!
 RewriteRule ^(.*)$ index.php [F,L]
 #
 ########## End - Rewrite rules to block out some common exploits
# Uncomment following line if your webserver's URL
 # is not directly related to physical file paths.
 # Update Your Joomla! Directory (just / for root)
########### Our New Rewrite Rules Start
RewriteBase /
RewriteCond %{HTTP_HOST} ^bowlerhatsolutions.co.uk [NC]
 RewriteRule ^(.*)$ http://www.bowlerhatsolutions.co.uk/$1 [L,R=301]
rewriterule ^sitemap\.html$ / [R=301,L]
 rewriterule ^web-site-promotion/pay-per-click.html /marketing/search-engines.html [R=301,L]
########### Our new rules end
########## Begin - Joomla! core SEF Section
 #
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_URI} !^/index.php
 RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$ [NC]
 RewriteRule (.*) index.php
 RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
 #
 ########## End - Joomla! core SEF Section
SetEnv DEFAULT_PHP_VERSION 5
DirectoryIndex index.cgi index.php
Easy?

So, to add your new mod_rewrite rules to the Joomla! .htaccess file you need to add them after the common exploit block and before the Joomla! core SEF section.

2. 500 Internal Server Error

If there is one other common error that we see crop up again and again with URL rewriting and specifically when adding rules to the joomla .htaccess file it is the dreaded 500 joomla internal server error.

If you have the joomla 500 error, the very first thing you need to do is ask your web host if .httaccess and mod_rewrite is supported. If so, then you can save yourself a lot of time by asking for a simple example .htaccess file configured for their server. If, they are not so helpful, and lets be honest here, technical support is generally useless then the following are a few things you should try:

1. Options +FollowSymLinks

Your website hosting may be set up to show content that is located in another location. This content is reached through the use of symbolic links (think windows shortcuts) so if you are having problems then try your .htacccess with and without this line:

Options +FollowSymLinks

2. RewriteBase

Where your the physical directory path does not match the web directory then your rewrites will fail. To get around this we use the RewriteBase directive to set the root of the URL path to match the current directory.

RewriteBase /

3. RewriteEngine on

This will generally be the first line in your .htaccess file and whilst it is nearly always going to be needed to enable the processing of rewrite rules it can be useful for testing purposes – if you are not 100% sure it is your rewrites causing the problem, then set RewriteEngine to Off: RewriteEngine Off.

RewriteEngine on

4.Misc Problems (Head scratchers)

If your htaccess file is working and it is your rules that are causing the problems then you need to methodically go through the rules one by one to find the problem. Chances are, you have an simple (but hard to spot) error so simply comment out all your rules and then uncomment them one at a time and test. Once you identify the problem rule check your syntax and compare it to your working rules and 9 times out of 10 you will find a simple, if not obvious mistake.

5. L = Last Rule

It seems fitting that the L – Last Rule Directive be the last thing that we look at. If you are searching the net and have followed one of the many tutorials out there for standard rewrites your code will likely look something like the below:

rewriterule ^page1.html /page2.html [R=301,L]

If we break this down we have the old page, the new page and then the directives in the square brackets. You likely know that the R=code provides the optional HTTP status code that we can use to tell the search engines or our visitors what kind of redirection this is. In the case of a 301 we know this is a permanent redirect where 302 is a temporary redirect that can be used during maintenance and the like.

The other, less commented on directive here is the ‘L’ which stands for Last Rule. If you have a series of rules and one triggers then you need to indicate that this is the last rule – watch out for this, many a 500 error can be averted and I myself hacked out some new rewrite rules a few days ago and forgot to add the L and wasted an hour before realising and repeatedly kicking myself.

Summary

Well, I sincerely hope this helps some of you poor soldiers working on the front line. Adding rewrite rules to the Joomla! htaccess file should be easy, but as with anything regarding .htaccess small problems can add up to lots of wasted hours.

Be Sociable, Share!

    Marcus Miller

    Marcus has been creating and marketing websites since, well, lets just say before the Millennium and is currently focused on Internet Marketing and SEO. If you have a problem, and no one else can help, and if you can find the contact page, Marcus can help. Email | Twitter | Google+

    Comments (4)

    • fonds de commerce

      |

      Bonjour.

      Le paragraphe de la redirection n’est pas clair en particulier ce qui concerne l’explication des 2 lignes :
      RewriteCond %{HTTP_HOST} !^www\.monsite\.com$ [NC]
      RewriteRule ^(.*) http://www.oonpub.com/$1 [QSA,L,R=301]

      Est il possible de préciser les explications ?

      Merci

      Reply

      • Marcus Miller

        |

        Hey, I must apologise but I don’t speak French. :( Google Translate tells me you are looking for clarification so here goes.

        It looks like you have two different sites there – is this two URls pointing at one site?

        Reply

    • vasanth

      |

      hi marcus,

      Have small confusion in REWRITE.
      The url is like that in joomla when i am enable the Search Engine Friendly URLs and Use URL rewriting..
      http://www.example.com/en/component/label

      But i need the URL like this..
      http://www.example.com/en/label

      what can i do in the .htaccess??

      Now i give like this
      RewriteEngine On
      RewriteRule ^en/component/label/ /en/label/ [R=301,L]

      But its not worked..

      Reply

      • Marcus Miller

        |

        Hey, this is really simple, turn on the URL rewriting but then create a menu link to the component and it will use that (nicer) alias. Hope that helps buddy! Marcus

        Reply

    Leave a comment