Redirecting all 404s to Joomla! properly
By default, the Joomla! htaccess-file allows you to redirect all SEF-requests to Joomla! so that Joomla! is respond with the proper content. This also counts for SEF URLs that do not exist: They will generate a 404-error, and this 404-error will also be picked up Joomla!. However, if the URL contains a dot (.), Joomla! will be bypassed and Apache will show a 404-error. This tutorial shows you how to modify the Joomla! htaccess-file to bypass this problem.
Which URLs are caught by Joomla!, and which are not?
The default Joomla! htaccess-file is designed to handle SEF URLs as they are generated by Joomla! itself. A SEF URL itself has no dots in it, no strange other characters (like dollar-signs or whatever). It looks a bit like this:
http://JOOMLA/my-category/my-subpage/example-article
Optionally, a HTML-suffix might have been configured within the Global Configuration:
http://JOOMLA/my-category/my-subpage/example-article.html
More file extensions that are fetched by Joomla!
So if there is no file-extension present, the Joomla! engine will be able to fetch a URL like this. But also if the file-extension is in the following list:
- .php
- .html or .htm
- .feed (for RSS-feeds)
- .raw
If you call an URL like this, it will generate a Joomla! 404-error:
http://JOOMLA/notexisting-folder/nonexisting-script.php
Note that if a CSS-file (.css) or an image (.jpg or .png) is called upon the browser, and that file actually exists, the Joomla! engine is also bypassed. But if the CSS-file does NOT exist, this default Joomla! behaviour makes sure the 404-error is served by Apache, and not Joomla!.
What is the difference between a Joomla! 404-error and an Apache 404-error?
Commonly, a Joomla! 404-error appears within the browser as a red-box. But using third party SEF-extensions like AceSEF or stand-alone extensions like Dynamic404 you can enhance this 404-error to be more friendly. The Apache 404-error is just a plain-text error, not served by Joomla! at all, and it is hard to modify this 404-error.
Why modify this default Joomla! behaviour?
It depends on what kind of files you are serving from within your site. If CSS-files and JavaScript-files are not found (so that they generate a 404-error), you need to fix this 404-error. Because CSS-files are only called from within the HTML-source of your pages, a 404-error belonging to a file like this will never end-up on somebodies browser-screen (unless they are actually debugging your site).
But if you are also serving Word-documents (docx, doc, odt, odx, etcetera), plain text-files (txt), movies (mpg, mpeg, avi) or archives (zip, bz2, tar, tgz), it might happen that a link is wrong, and that your visitor will get an ugly Apache-error when they click on an invalid link.
So instead of just serving only the default file-extensions from Joomla! (php, html, htm, feed, pdf, raw), you might want to modify the Joomla! htaccess-file to serve all (!) file-extensions from Joomla! except from the ones that really don't make sense (css, js,
Locating the section to modify
Open up the file .htaccess within a text-editor and locate the following section:
########## 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]
#
Modifying the section
Now replace the following line:
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$ [NC]
With this:
RewriteCond %{REQUEST_URI} !(\.jpg|\.jpeg|\.gif|\.png|\.css|\.js)$ [NC]
#RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$ [NC]
After the fix
Voila, if you now type in a non-existing URL with dots in the URL it will be served by Joomla! - hence generating a Joomla! 404-error:
http://JOOMLA/nonexisting/file.txt
Only when CSS-files and such are called, will the original Apache 404-error appear:
http://JOOMLA/templates/mytemplate/css/notexisting.css
This tutorial will work with any 404-tool, and therefor also with our own Yireo Dynamic404 extension.
Created on Wednesday, 23 November 2011Modified on Wednesday, 23 November 2011
More tutorials in this section
- Joomla! 1.6 ACLs (1): Marketing group
- Load a module within your Joomla! articles
- Modifying a Joomla! language pack
- Redirect html-suffix in your URLs
- Video: Speed Up Joomla with mod_deflate
- Hiding PHP Notices in Joomla!
- So what's happening with Joomla! 1.6?
- Get rid of /index.php/ in your Joomla! URLs
- Redirecting all 404s to Joomla! properly
- Dealing with SSL mixed content in Joomla!
- Troubleshooting a blank page
