2

I'm currently building a gallery in Kirby and have an album page that effectively has two modes: 1) Gallery listing, showing all items. 2) Single Image. I'm passing in a GET variable like so:

/gallery/album-name?p=03.jpg

What I really want is a URL like this:

/gallery/album-name/03.jpg

Kirby already does some url rewriting in its default .htaccess file, like so:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php [L]

Which re-writes the urls from this /index.php/gallery/album-name?p=03.jpg removing the index.php part.

Now, I did think I could simply have it rewrite any URL that has a get variable like this:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/([A-Za-z0-9-]+)?$ index.php?p=$1 [L]

But that didn't seem to work.

My question is this: Can this be done? And if so, will the GET variable still be available to my script?

Many thanks!


My current full re-write rules

<IfModule mod_rewrite.c>

RewriteEngine on
RewriteBase /

#//www. vs //
RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

# block text files in the content folder from being accessed directly
RewriteRule ^content/(.*)\.(txt|md|mdown)$ error [R=301,L]

# block all files in the site folder from being accessed directly
RewriteRule ^site/(.*) error [R=301,L]

# block all files in the kirby folder from being accessed directly
RewriteRule ^kirby/(.*) error [R=301,L]

# make panel links work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^panel/(.*) panel/index.php [L]

# make gallery links work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^gallery/(.*)/([A-Za-z0-9-]+\.jpg)?$ index.php?p=$2 [L]

# make site links work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php [L]

</IfModule>

This works for everything, but I get a 404 on (for example) this url: /gallery/my-gallery-name/01.jpg

  • I'd suggest you to not change the rewrite rules but instead use the params functionality coming with kirby: http://getkirby.com/blog/the-mighty-mighty-uri-object – lulezi Jun 24 '13 at 20:24

2 Answers2

1

Try this, it will work

RewriteRule ^our-wedding/(.*)$ index.php?p=$1 [L]

Now you can access it via

/gallery/our-wedding/03.jpg
Sumit Bijvani
  • 8,154
  • 17
  • 50
  • 82
  • Thanks, but the /our-wedding/ part of the `URL` is already rewritten by the .htaccess. The base URL looks like this: `mysite.com/index.php/gallery/our-wedding/?p=01.jpg`. There are also many galleries. – Jon Roobottom Mar 20 '13 at 19:04
  • I imagine with this method I'd have to add a rule every time I add a gallery. – Jon Roobottom Mar 20 '13 at 19:10
0

Your RewriteRule has a regular expression with two parts

^(.*)/([A-Za-z0-9-]+)?$
  |    |
  $1   $2

$1 is the gallery part and $2 is the optional picture part. You can use only one of those parts or both, whatever you need. If you want to use only the picture part, you must use $2 in your rewrite rule

RewriteRule ^(.*)/([A-Za-z0-9-]+)?$ index.php?p=$2 [L]

Although, your image part regular expression does not match 03.jpg. If you want to match that, you must include a dot . in the character class [...] or add an extension to it

[A-Za-z0-9-]+\.jpg
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
  • Thanks for that, but I still can't get the thing to work. I've matched using `^gallery` as using `^(.*)` meant my other links around the site stopped working (like `/about`, etc)... I've pasted my full rewrite rules in the question. – Jon Roobottom Mar 21 '13 at 00:09
  • @JonRoobottom This should work. Do you have `index.php` in the root directory? – Olaf Dietsche Mar 21 '13 at 07:33
  • Yes. I don't know if [Kirby](http://getkirby.com) is doing any other funky stuff. I posted my full .htacccesss rewrite rules above, maybe there's some conflict there? Thanks! – Jon Roobottom Mar 21 '13 at 09:12
  • @JonRoobottom I have already seen your .htaccess. I tested it in my environment and it works here. Can you do some `echo $_GET['p']` in your script, to see, if it is called? Maybe the 404 comes from script and not from the .htaccess. Can you see the rewrite log, if there's something going wrong? – Olaf Dietsche Mar 21 '13 at 09:29
  • I did a var dump of `GET` vars on my error page, but none were being passed to the page. I don't have a physical directory called `gallery` everything runs through the `index.php` file. The combination of the .htaccess and Kirby doing some internal redirection is causing this not to work. – Jon Roobottom Mar 22 '13 at 16:05
  • @JonRoobottom So, the `index.php` seems to be called. This is good, because then the rewrite rules work. Just a nitpick `GET` vs `_GET`. You can also try `phpinfo(INFO_VARIABLES);`. This shows the `_GET` and all other variables available. – Olaf Dietsche Mar 23 '13 at 14:08