Note: I tried to post this hack in the hacks/mods forum but it's moderators only. Please move it if appropriate. Thanks!
In the last few hours I've been busy turning all my site URLs from this:
http://lomoblog.com/displayimage.php?album=17&pos=27into this
http://lomoblog.com/album/17/img/27I've made all my public urls search engine friendly and also more human readable and understandable.
All admin urls were left untouched because there's really no need to mess with those.
So before I proceed, a disclaimer:
- I'm no mod_rewrite whiz. I have google and a lot of patience.
- I'm not a php programmer. I can read, I have google and a lot of patience.
- This solution fits my needs. If it fits yours, good. If not, you shouldn't try this, since I'm unable to help.
- If you are not comfortable messing with code, do not try to apply this.
- If you have any suggestions, don't be shy!
CPG has a nice feature that saves a lot of sweat to any webmaster: it resides inside its own dir and hasn't any link dependancies outside, e.g., all links point to cpg directory.
Making friendly urls breaks this and forces you to recode/re-link a lot of files/links even though not a single file will be moved from its current location.
My CPG installation resides in my site's root which helps me a lot: adding a single forward slash in the beginning of each link solves most of my problems.
I've changed 5 files to make this work on my site. Since my CPG is already heavily hacked, I can't guarantee it will not need another tweak somewhere else.
1 - .htaccess
I've added the following code to my .htaccess
RewriteEngine on
#language
RewriteRule ^lang/([a-z0-9]*)/?$ index.php?lang=$1[L]
RewriteRule ^([a-z0-9]*)/([a-z0-9]*)/lang/([a-z0-9]*)/?$ $1/$2&lang=$3 [L]
RewriteRule ^(.+)/(.+)/(.+)/(.+)/lang/([a-z0-9]*)/?$ $1/$2/$3/$4&lang=$5 [L]
RewriteRule ^(.+)/(.+)/(.+)/(.+)/(.+)/(.+)/lang/([a-z0-9]*)/?$ $1/$2/$3/$4/$5/$6&lang=$7 [L]
#RewriteRule ^([0-9a-z]+)?/?([0-9a-z]+)?/?([0-9a-z]+)?/?([0-9a-z]+)/lang/([a-z0-9]*)/?$ $1/$2/$3/$4&lang=$5
#albums
RewriteRule ^album/([a-z0-9]*)/?$ thumbnails.php?album=$1
RewriteRule ^album/([a-z0-9]*)/page/([0-9]*)/?$ thumbnails.php?album=$1&page=$2
RewriteRule ^album/([a-z0-9]*)/sort/([a-z]*)/?$ thumbnails.php?album=$1&sort=$2
RewriteRule ^album/([a-z0-9]*)/page/([0-9]*)/sort/([a-z]*)/?$ thumbnails.php?album=$1&page=$2&sort=$3
RewriteRule ^category/([a-z0-9]*)/?$ index.php?cat=$1
RewriteRule ^album/([a-z0-9]*)/category/([a-z0-9]*)/page/([0-9]*)/?$ thumbnails.php?album=$1&cat=$2&page=$3
#image
RewriteRule ^img/([0-9-]*)/?$ displayimage.php?pos=$1
RewriteRule ^album/([a-z0-9]*)/category/([0-9-]*)/?$ thumbnails.php?album=$1&cat=$2
RewriteRule ^album/([a-z0-9]*)/category/([0-9-]*)/img/([0-9-]*)/?$ displayimage.php?album=$1&cat=$2&pos=$3
RewriteRule ^album/([a-z0-9]*)/img/([0-9-]*)/?$ displayimage.php?album=$1&pos=$2
#search
RewriteRule ^search/([a-zA-Z0-9]*)/?$ thumbnails.php?album=search&search=$1
RewriteRule ^search/([a-zA-Z0-9]*)/?$ thumbnails.php?album=search&type=full&search=$1
2 - template.html
Small modifications. Basically just adding forward slashs to the beginning of SRCs like this one:
<link rel="stylesheet" href="
/themes/default/style.css" />
This will ensure that all files have relative paths from the site's root.
3 - index.php
Two small modifications here:
$link = $ident . "<a href=\"index.php?cat={$subcat['cid']}\">{$subcat['name']}</a>";
for this:
$link = $ident . "<a href=\"/category/{$subcat['cid']}\">{$subcat['name']}</a>";
and this:
$link = $ident . "<a href=\"index.php?cat={$subcat['cid']}\">{$subcat['name']}</a>";
for this:
$link = $ident . "<a href=\"/category/{$subcat['cid']}\">{$subcat['name']}</a>";
4 - displayimage.php
Lots of modifications here. All
a hrefs need a forward slash at the beginning and almost all variables need tweaking.
Example:
$thumb_tgt = "thumbnails.php?album=$album$cat_link&page=$page";
replaced by:
$thumb_tgt = "/album/$album$cat_link/page/$page";$thumb_tgt = "/album/$album$cat_link/page/$page";
and finally, 5 - theme.php
In theme.php it's a bit tricky. Leave alone all
a hrefs pointing to variables (example: <a href="{ALB_LINK_TGT}">) and add a forward slash in all
a hrefs pointing to files (example: <a href="/delete.php?...>)
Change all variables like:
$cat_l = isset($cat) ? "?cat=$cat" : '';
to became:
$cat_l = isset($cat) ? "/category/$cat" : '';
I've changed
cat with
category and
pos with
img.
To know how to replace urls, use the code above (in .htaccess section) as a guide, example: replace
thumbnails.php?album=$album with
/album/$album or
displayimage.php?album=$album&pos=$id for
album/$album/img/$idI'm sorry this isn't a bit more complete but I'm very tired and wanted to write this while it was fresh on my mind.
Ricardo