Advanced search  

News:

cpg1.5.48 Security release - upgrade mandatory!
The Coppermine development team is releasing a security update for Coppermine in order to counter a recently discovered vulnerability. It is important that all users who run version cpg1.5.46 or older update to this latest version as soon as possible.
[more]

Pages: [1] 2 3   Go Down

Author Topic: mod_rewrite crash course: what and how  (Read 61833 times)

0 Members and 1 Guest are viewing this topic.

rbl

  • Contributor
  • Coppermine regular visitor
  • ***
  • Offline Offline
  • Posts: 60
    • http://lomoblog.com/
mod_rewrite crash course: what and how
« on: October 27, 2003, 06:30:11 am »

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=27
into this
http://lomoblog.com/album/17/img/27
I'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
Code: [Select]
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:
Code: [Select]
$link = $ident . "<a href=\"index.php?cat={$subcat['cid']}\">{$subcat['name']}</a>";for this:
Code: [Select]
$link = $ident . "<a href=\"/category/{$subcat['cid']}\">{$subcat['name']}</a>";and this:
Code: [Select]
$link = $ident . "<a href=\"index.php?cat={$subcat['cid']}\">{$subcat['name']}</a>";for this:
Code: [Select]
$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:
Code: [Select]
$thumb_tgt = "thumbnails.php?album=$album$cat_link&page=$page";replaced by:
Code: [Select]
$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:
Code: [Select]
$cat_l = isset($cat) ? "?cat=$cat" : '';to became:
Code: [Select]
$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/$id

I'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
« Last Edit: March 27, 2005, 01:04:56 pm by GauGau »
Logged

copperminerules

  • Coppermine newbie
  • Offline Offline
  • Posts: 2
mod_rewrite crash course: what and how
« Reply #1 on: October 29, 2003, 12:47:33 am »

has anyone been successful with this hack?

i havnt... but i put that down to lack of php knowledge!
Logged

Joachim Müller

  • Dev Team member
  • Coppermine addict
  • ****
  • Offline Offline
  • Gender: Male
  • Posts: 47844
  • aka "GauGau"
    • gaugau.de
mod_rewrite crash course: what and how
« Reply #2 on: October 29, 2003, 09:08:07 am »

possibly it's related to the question: does your webhost allow you to do this magic on his server? Budget webhosting often comes with limitations on these issues. Don't worry - aks your webhost if you're allowed to use the "RewriteEngine".

GauGau
Logged

rbl

  • Contributor
  • Coppermine regular visitor
  • ***
  • Offline Offline
  • Posts: 60
    • http://lomoblog.com/
mod_rewrite crash course: what and how
« Reply #3 on: October 29, 2003, 12:09:44 pm »

One simple way to see if it works is to start with something simple like language or search because you only need to change your .htaccess file and nothing else.
For example, add the following to your .htaccess file:
Code: [Select]
RewriteEngine on
#language
RewriteRule ^lang/([a-z0-9]*)/?$   index.php?lang=$1

and visit your CPG like:
http://your.domain.tld/path_to_coppermine/lang/french
and
http://your.domain.tld/path_to_coppermine/lang/english

You need, of course, to have the language files installed =)

You can try it on my site:
http://lomoblog.com/lang/french
http://lomoblog.com/lang/english
Logged

Oasis

  • VIP
  • Coppermine frequent poster
  • ***
  • Offline Offline
  • Gender: Male
  • Posts: 252
  • iNSiGNiA
    • Pixnet Gallery
mod_rewrite crash course: what and how
« Reply #4 on: October 29, 2003, 04:54:20 pm »

if you try the language rewrite on the last post and it works, your images will most likely be broken, but if the page shows up you will know modrewrite is enabled.
Logged
Pixnet Gallery: http://www.pixnet.net
iNSiGNiA Weblog: http://www.jayliu.org

blueyed

  • Coppermine newbie
  • Offline Offline
  • Posts: 4
    • http://thequod.de
mod_rewrite crash course: what and how
« Reply #5 on: November 13, 2003, 05:06:02 pm »

I'd like to see support for mod_rewrite in the release..

There could be sth like
Code: [Select]
$CONFIG['fancy_urls'] = true;
$CONFIG['root_url'] = '/cpg/';

root_url would then be put at front of all paths (like the ones in template.html)..
and fancy_urls would be checked at places where link generation is done.

Or these settings could go to the admin's config part, where there could also be a button 'show mod_rewrite source', which put all necessary lines together.. ready to go into .htaccess.

That would be really sweet..  :)
Logged

gmarik

  • Coppermine novice
  • *
  • Offline Offline
  • Posts: 34
mod_rewrite crash course: what and how
« Reply #6 on: January 18, 2004, 12:31:43 pm »

This one really helped me - this sdould be in the default, standart CPG edition, don't you think so? As an option from the CP.
Logged
webmaxtor.com

Joachim Müller

  • Dev Team member
  • Coppermine addict
  • ****
  • Offline Offline
  • Gender: Male
  • Posts: 47844
  • aka "GauGau"
    • gaugau.de
mod_rewrite crash course: what and how
« Reply #7 on: January 18, 2004, 12:38:23 pm »

I don't think so, as it'll only work for users on apache webservers, and we don't want to develop for a single platform. Including options that will only work on certain platforms will confuse most beginners.

I don't want to start a flame thread on this: it's my personal opinion that apache is much better than any other webserver; if you observe my postings from the past  I have the attitude to recommend to people not to use IIS (and not to run their own server if they have no idea what they're doing), but after all it's up to people - they'll have to live with IIS (and for some it's not even an option).

GauGau
Logged

invision

  • Coppermine newbie
  • Offline Offline
  • Posts: 3
mod_rewrite crash course: what and how
« Reply #8 on: January 24, 2004, 04:26:12 pm »

:oops: don't work for me ! :D i got error when use it  :lol:  :cry:  :oops:  :twisted:
Logged

Joachim Müller

  • Dev Team member
  • Coppermine addict
  • ****
  • Offline Offline
  • Gender: Male
  • Posts: 47844
  • aka "GauGau"
    • gaugau.de
mod_rewrite crash course: what and how
« Reply #9 on: January 25, 2004, 08:45:09 am »

maybe your host doesn't let you have it. For details, post the error (a link might help as well).

GauGau
Logged

MaThIbUs

  • Contributor
  • Coppermine frequent poster
  • ***
  • Offline Offline
  • Gender: Male
  • Posts: 130
  • I rock.
    • http://mathibus.com/
mod_rewrite crash course: what and how
« Reply #10 on: February 15, 2004, 01:17:46 pm »

Thanks for the great hack, rbl!

Some additions:

  • Put this on top of your .htaccess file if you get 403 errors (invision, this might help you!):

    Code: [Select]
    Options +SymLinksIfOwnerMatch

  • By the way, there's no need to do all this forward slashing crap :P Just open up /themes/yourtheme/template.html and add this between your <head></head> tags:

    Code: [Select]
    <base href="http://www.yoursite.com/coppermine/" />

  • I wanted to generate absolute URLs like this {http://lomoblog.com/img/46/} instead of like this {http://lomoblog.com/img/-46}. I replaced the first line of the #image part of the .htaccess file with this:

    Code: [Select]
    #image
    RewriteRule ^([0-9-]*)/?$   displayimage.php?pos=-$1
    [/list:u]
    P.S.: I might be mistaken, but isn't this two times the same? (the replacement code)

    Quote from: "rbl"
    4 - displayimage.php
    Lots of modifications here. All a hrefs need a forward slash at the beginning and almost all variables need tweaking.
    Example:
    Code: [Select]
    $thumb_tgt = "thumbnails.php?album=$album$cat_link&page=$page";
    replaced by:
    Code: [Select]
    $thumb_tgt = "/album/$album$cat_link/page/$page";$thumb_tgt = "/album/$album$cat_link/page/$page";[/color]
Logged

Hein Traag

  • Dev Team member
  • Coppermine addict
  • ****
  • Country: nl
  • Offline Offline
  • Gender: Male
  • Posts: 2166
  • A, B, Cpg
    • Personal website - Spintires.nl
mod_rewrite crash course: what and how
« Reply #11 on: February 23, 2004, 08:17:29 pm »

As i am a complete n00b at hacking does anyone have the modified files in a zip somewhere ?
Logged

Joachim Müller

  • Dev Team member
  • Coppermine addict
  • ****
  • Offline Offline
  • Gender: Male
  • Posts: 47844
  • aka "GauGau"
    • gaugau.de
mod_rewrite crash course: what and how
« Reply #12 on: February 24, 2004, 08:16:19 am »

it was stated before, but be warned again: this mod is not meant for you if you don't know at all what you're doing. If you're a newbie, don't try this, unless you can live with your website being inaccessible for some time...

GauGau
Logged

Hein Traag

  • Dev Team member
  • Coppermine addict
  • ****
  • Country: nl
  • Offline Offline
  • Gender: Male
  • Posts: 2166
  • A, B, Cpg
    • Personal website - Spintires.nl
mod_rewrite crash course: what and how
« Reply #13 on: February 24, 2004, 09:40:28 am »

Whoo supportive. What i'm trying to point out is that the above is about modifying some files of the coppermine gallery.. which might as well be made downloadable as a package imho as a service to those who would like to simplify their urls but are not very skilled (yet) as some of the more experienced users on this board.

You can't expect everyone to be as skilled as the developers or contributors to coppermine now can you ?
Logged

hyperion

  • VIP
  • Coppermine addict
  • ***
  • Offline Offline
  • Posts: 1317
  • - retired -
mod_rewrite crash course: what and how
« Reply #14 on: February 24, 2004, 09:58:43 am »

This is not a mod that can easily be put in a zip file and expected to work for everyone who downloads it. If you can't follow the the thread and understand it, you have no business attempting this modification. You are welcome to try it, but do not complain if your entire website goes down.

Mind your manners.
Logged
&quot;Then, Fletch,&quot; that bright creature said to him, and the voice was very kind, &quot;let&#039;s begin with level flight . . . .&quot;

-Richard Bach, Jonathan Livingston Seagull

(http://www.mozilla.org/products/firefox/buttons/getfirefox_small.png)

Hein Traag

  • Dev Team member
  • Coppermine addict
  • ****
  • Country: nl
  • Offline Offline
  • Gender: Male
  • Posts: 2166
  • A, B, Cpg
    • Personal website - Spintires.nl
mod_rewrite crash course: what and how
« Reply #15 on: February 24, 2004, 11:09:40 am »

I don't agree entirely on this. Sure it would not work for everyone but since the internet is big there is bound to be a fairly large group for whom it will work.

And since it would make coppermine more user friendly i see no reason why there shouldn't be a option to include this in coppermine (at your own risk).  

I did not mean to be impolite or anything before but if the only reason not to include a option in coppermine that shortens the urls is that it would not work for some people then your limiting the the spread of coppermine imho.
Logged

ariev

  • Coppermine newbie
  • Offline Offline
  • Posts: 10
mod_rewrite crash course: what and how
« Reply #16 on: April 13, 2004, 03:24:36 am »

I'm not promoting this exactly, but found it as a utility that will enable long URLs to be setup as anything you want. I haven't used the script but it looks good.

http://www.scripts24.com/iredirector/subdirectory/index.php

I guess the same thing could be done with mod_rewrite too.
Logged

kaitou

  • Coppermine newbie
  • Offline Offline
  • Posts: 6
Re: mod_rewrite crash course: what and how
« Reply #17 on: April 27, 2004, 07:39:31 am »

Heh nice, very cool hack.  ;D
Definetly not for the php illiterate.
Just got it working at my site: http://www.animegalleries.net
Only thing I can't find, is where the breadcrumb is generated, to hack those url's also. Can someone point me to the file where I could find that?

And I would -love- to see this included in the next version of coppermine. I know you said you don't want to have features that only work for some people, to avoid confusions, but you could have the option detect if it is on a apache server or not, and if not, it wouldn't even show up in the admin? And then just ask if to enable it or not, same way as one has to pick GD or ImageMagik.
« Last Edit: April 27, 2004, 08:12:43 am by kaitou »
Logged
(http://www.tuxedomask.com/sig/sigbox.jpg)

binocle

  • Coppermine newbie
  • Offline Offline
  • Posts: 19
    • http://www.surf-bzh.com
Re: mod_rewrite crash course: what and how
« Reply #18 on: May 18, 2004, 09:09:57 pm »

Thank you very much,

I use this hack in a different way here :

http://www.surf-bzh.com/coppermine_dir/index.php

I have not finished yet, i have some problems for displaying image, but I work on it !

my main problem is about the url of category of  the table given in the top of album and image

example :

(http://www.surf-bzh.com/divers/cop1.jpg)

I don't find the variables to change ?

Can you help me  ;)

thanks
Logged

binocle

  • Coppermine newbie
  • Offline Offline
  • Posts: 19
    • http://www.surf-bzh.com
Re: mod_rewrite crash course: what and how
« Reply #19 on: June 01, 2004, 10:16:48 pm »

bump ???
Logged
Pages: [1] 2 3   Go Up
 

Page created in 2.073 seconds with 18 queries.