Website Image

Adding Additional Rewrite Rules to Joomla .htaccess

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 are clearly shown with an 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 let’s 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 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 (Headscratchers)

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 a 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.

 

8 Comments
  • fonds de commerce
    Posted at 16:01h, 17 December Reply

    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

    • Marcus Miller
      Posted at 11:10h, 02 January Reply

      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?

  • vasanth
    Posted at 07:00h, 08 February Reply

    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..

    • Marcus Miller
      Posted at 16:57h, 08 February Reply

      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

      • David S
        Posted at 05:30h, 26 August Reply

        Hey Marcus,

        Could you help me out? I have this same problem, but I can’t simply link a menu to the component.

        – Enabled the Search Engine Friendly URLs and Use URL rewriting
        Joomla gives me
        http://www.example.com/listing/1-property-alias
        Everything fine.

        What I need is:
        http://www.example.com/customtext/property-alias.htm

        So I have set my htaccess as this:
        RewriteRule ^customtext/property-alias.htm?$ listing/1-property-alias [NC,L]

        But it doesn’t pick it up, what does my htaccess need to have? 🙂

        – I tried placing it before and after the Joomla SEF in htaccess but no luck? Other normal rewrite rules are fine, like to actual files on the server.

        Cheers,
        David

        • Marcus Miller
          Posted at 15:10h, 15 February Reply

          Hey David, this comment got lost in the wash. Drop me an email if you are still struggling and I will see if I can help.

  • Anders Frohm
    Posted at 13:15h, 13 December Reply

    Hi I’ve added the following right just below RewriteEngine On:

    RewriteCond %{QUERY_STRING} (^|&)w=[0-9]+
    RewriteRule ^images/(.*.jpg)$ templates/oasrorelsen/img.php?src=$1 [QSA,NC]

    But for some reason this breaks URL rewriting for Joomla. Please give me som advice:

    Thanks in advance!

    • Marcus Miller
      Posted at 13:26h, 13 December Reply

      Hey Anders – this post is so, so, so old I could not be sure that is even good advice – likely from around 2010. We have not tinkered with Joomla for a few years. Sorry can’t help more! Marcus

Post A Comment