
Search engine friendly URLs - Pixaria 1.x
|
Table of Contents
|
These instructions are for Pixaria 1.5.3 and earlier. For help using search engine friendly URLs with newer versions of Pixaria, please see the documentation page for Pixaria 2.0.
What is Mod_Rewrite?
First off, if you don't have Apache installed as the web server daemon on the computer where Pixaria Gallery is hosted, then this tutorial is not for you. Mod_Rewrite is a very powerful module for the Apache web server that allows you to do complex, rule based URL redirections to dynamically control how a web browser accesses resources on your web server.
This part of the documentation is not intended to provide a guide to using Mod_Rewrite because there are plenty of guides out there that will do a much better job of it than I can! The best place to begin if you're looking for more information is Google.
Note, Mod_Rewrite support is only available in Pixaria Gallery 1.0.4 and above.
What does Mod_Rewrite do for Pixaria Gallery?
Query strings and complex URLs
If you've already got Pixaria Gallery up and running on your server, you'll probably have noticed that lots of the URLs for images and albums are complex, long lines of text with question marks, ampersands, equals signs and other bits and pieces in them. These types of URLs are interpreted by Pixaria Gallery as a means of transferring information between pages without the need for submitting a form.
For example, the following code after the end of a normal URL for a PHP script like 'index.php' will tell the script that there is a new variable to use called 'id' with the value of '300': ?id=300. It's possible to pass lots of variables into a PHP script by building chains of these so-called query strings with each variable name and value separated by an ampersand (&) sign.
As you will no doubt have noticed, these complex strings of variables in a URL make it virtually impossible to remember an exact URL for an image in Pixaria Gallery but there is a more serious consequence in that these URLs are almost never indexed by search engines. The reason for this is that most search engine spiders will correctly assume that a link containing a query string is a link to a dynamic page i.e. one that will have different content depending on the variables passed in the query string.
This is obviously a bad thing if you want search engine spiders to index your site so people can find your images.
How can we avoid using query strings
There are two ways of avoiding using query strings:
- Don't use dynamic links and pages
- Make fake non dynamic links that are really pointers to dynamic links and pages
The first solution, while simple for small sites, is really not at all practical for a script like Pixaria Gallery where there are potentially hundreds or thousands of images each needing their own separate page. Some scripts get around this by outputting real HTML pages with static links so the pages are not generated dynamically at all. This is a fairly good solution but it means that whenever the site is changed, all the pages have to be created again which is time consuming and demanding on the serve, particularly with respect to storage space.
The second solution is much better but is a little more complex to implement...
Mod_Rewrite to the rescue
Mod_Rewrite is the key to this solution and while most web servers using Apache to serve pages support it, some don't. For this reason, it's not always possible to use this technique with all web servers and hosting packages.
To describe what Pixaria Gallery can do with Mod_Rewrite is quite simple:
Basically, it becomes possible to make a static URL like:
'http://www.pixaria.com/gallery/2345'
Point, internally, to a dynamic URL like:
'http://www.pixaria.com/index.gallery.php?gid=2345'
This means that the user (or search engine spider) thinks the page is static when in fact is isn't. At no point will the user be aware that the URL in the address bar of their web browser ('http://www.pixaria.com/gallery/2345') is actually a PHP script. Since all the URL rewriting is done on the web server the process is transparent and it becomes possible to have an unlimited number of fake URLs that all get translated into requests for a script.
Setting up Mod_Rewrite
Please note that editing the files described here can cause your website to fail completely if you don't know what you're doing and make a mistake. You follow these set-up instructions entirely at your own risk, so please make back-ups and if in doubt, don't do anything!
Before you can use Mod_Rewrite with Pixaria Gallery, your Apache web server will need configuring with various settings to enable it to integrate with the requirements for URL rewriting in Pixaria Gallery. There are two ways in which you can make these configuration changes. The first is to edit the httpd.conf file and the second is to use the Apache .htaccess file.
It's very important that the mod_rewrite module for Apache is loaded in httpd.conf before you try to add any of the mod_rewrite directives to either httpd.conf or to a .htaccess file. If it isn't then Apache will not run correctly and your website will not be available. For this reason, I recommend consulting your hosting company or system administrator about using Mod_Rewrite before you begin making the changes in this tutorial.
Mod_Rewrite can usually be enabled in httpd.conf by uncommenting these lines:
In Apache 1.x
#LoadModule rewrite_module /usr/lib/apache/mod_rewrite.so
#AddModule mod_rewrite.c
Like this:
LoadModule rewrite_module /usr/lib/apache/mod_rewrite.so
AddModule mod_rewrite.c
In Apache 2.x
#LoadModule rewrite_module /usr/lib/apache/mod_rewrite.so
Like this:
LoadModule rewrite_module /usr/lib/apache/mod_rewrite.so
AddModule mod_rewrite.c
Note that these entries don't usually appear on lines next to each other like this in the actual config file and the paths shown may differ depending on how your server is configured and what operating system it uses.
Editing httpd.conf
If you have access to your httpd.conf file, you will need to add the following code to the file AFTER the part where you define the DocumentRoot and ServerName:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/gallery/([0-9]+)/image/([0-9]+) /index.gallery.php?gid=$1&img=$2 [L]
RewriteRule ^/gallery/([0-9]+)/pages/([0-9]+) /index.gallery.php?gid=$1&gpg=$2 [L]
RewriteRule ^/gallery/([0-9]+)/([0-9]+) /index.gallery.php?gid=$1&pg=$2 [L]
RewriteRule ^/gallery/([0-9]+) /index.gallery.php?gid=$1 [L]
RewriteRule ^/pages/([0-9]+) /index.php?gpg=$1 [L]
</IfModule>
These rules will firstly turn the Rewrite engine on and then map various gallery URLs in your Pixaria Gallery installation to the appropriate PHP scripts. If you decide to change the name of any of the directories in these rules, you MUST also change them in the file 'psg.settings.inc.php' in the section titled Mod_Rewrite.
Once httpd.conf has been saved and uploaded to the server, you will need to restart the Apache process using whatever systems are provided by your hosting company (telnet, ssh, web control panel etc...).
Using a .htaccess file
If you don't have access to httpd.conf, you can also paste the mod_rewrite code into a file called .htaccess and upload this file into the root of your site's domain. It is most important that you check to see if there are any .htaccess files already on your server and beware that some FTP programs and servers hide these from view which means that it's possible to accidentally overwrite a file that was already there and potentially mess up your server configuration.
For .htaccess, you should use the following code:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/gallery/([0-9]+)/image/([0-9]+) /index.gallery.php?gid=$1img=$2 [L]
RewriteRule ^/gallery/([0-9]+)/pages/([0-9]+) /index.gallery.php?gid=$1&gpg=$2 [L]
RewriteRule ^/gallery/([0-9]+)/([0-9]+) /index.gallery.php?gid=$1&pg=$2 [L]
RewriteRule ^/gallery/([0-9]+) /index.gallery.php?gid=$1 [L]
RewriteRule ^/pages/([0-9]+) /index.php?gpg=$1 [L]
</IfModule>
Using Mod_Rewrite for installations in a subdirectory
If your Pixaria Gallery site is in a subdirectory of your root domain name (eg. http://www.mysite.com/subdir/), use this code, replacing 'subdir' with the name of your subdirectory:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/subdir/gallery/([0-9]+)/image/([0-9]+) /subdir/index.gallery.php?gid=$1&img=$2 [L]
RewriteRule ^/subdir/gallery/([0-9]+)/pages/([0-9]+) /subdir/index.gallery.php?gid=$1&gpg=$2 [L]
RewriteRule ^/subdir/gallery/([0-9]+)/([0-9]+) /subdir/index.gallery.php?gid=$1&pg=$2 [L]
RewriteRule ^/subdir/gallery/([0-9]+) /subdir/index.gallery.php?gid=$1 [L]
RewriteRule ^/subdir/pages/([0-9]+) /subdir/index.php?gpg=$1 [L]
</IfModule>
Configuring Pixaria Gallery
Once you have got the Apache bits working, you can now turn on Mod_Rewrite support in Pixaria Gallery. This is controlled by a setting in the general settings control panel.
All you have to do is check the checkbox to turn Mod_Rewrite support on and then save the changes. You will now be able to browse your site as normal using rewritten URLs.
Troubleshooting Mod_Rewrite
If you find that Mod_Rewrite is not working correctly after applying your .htaccess or httpd.conf settings and updating your Pixaria Gallery site settings, you may need to edit the form of the rewrite rules for Apache on your web server.
One problem which can occur is if your Pixaria installation directory is an alias setup using the Apache Alias directive. In this case, use the following format for your rewrite rules:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/gallery/([0-9]+)/image/([0-9]+) /subdir/index.gallery.php?gid=$1&img=$2 [PT,L]
RewriteRule ^/gallery/([0-9]+)/pages/([0-9]+) /subdir/index.gallery.php?gid=$1&gpg=$2 [PT,L]
RewriteRule ^/gallery/([0-9]+)/([0-9]+) /subdir/index.gallery.php?gid=$1&pg=$2 [PT,L]
RewriteRule ^/gallery/([0-9]+) /subdir/index.gallery.php?gid=$1 [PT,L]
RewriteRule ^/pages/([0-9]+) /subdir/index.php?gpg=$1 [PT,L]
</IfModule>
A second issue is that some servers are set up in such a way that you must omit the leading slash character at the start of each rule like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^gallery/([0-9]+)/image/([0-9]+) /index.gallery.php?gid=$1&img=$2 [L]
RewriteRule ^gallery/([0-9]+)/pages/([0-9]+) /index.gallery.php?gid=$1&gpg=$2 [L]
RewriteRule ^gallery/([0-9]+)/([0-9]+) /index.gallery.php?gid=$1&pg=$2 [L]
RewriteRule ^gallery/([0-9]+) /index.gallery.php?gid=$1 [L]
RewriteRule ^pages/([0-9]+) /index.php?gpg=$1 [L]
</IfModule>
Created: Wednesday, May 10, 2006
Last edited: Wednesday, October 24, 2007
Tools & Utilities
Third Party Resources
|
Random Tips & Tricks
#6 Flush the cache Caching Smarty templates improved the performance of your site but if you're editing templates, you need to to turn off template caching or to flush the Smarty template cache before you try to view changes you've made to a template. Caching options can be found in the appearance control panel of your site. |
