0

Possible Duplicate:
Rewriting an arbitrary number of path segments to query parameters

I'm sure this has been asked many times but I don't actually know what it is I'm searching for, so if someone could just point me to the correct place that would be brilliant.

Basically, I have a page "gallery.php" which at the moment loads images from directories (first part is hard coded) then in the url you'd type ?a=art/paris, so the full URL is gallery.php?a=art/gallery.

Is there a way I can modify apache and/or my php code so the user can type in /gallery/art/paris/ or /gallery/art/paris and get the same result? (anything after /gallery/ is the one parameter I need.

Community
  • 1
  • 1
THEK
  • 740
  • 2
  • 10
  • 29

3 Answers3

3

Try putting this in your .htaccess:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-F

RewriteRule ^gallery/([a-z-A-Z0-9\/]+)$ /gallery.php?a=$1 [NC]
Gerben
  • 111
  • 1
  • 8
  • That's almost what I need but it doesn't work with /art/paris. It only passes /art as the parameter. I need it to pass /art/paris as the parameter. – THEK Oct 26 '12 at 17:15
  • (1) Those three RewriteCond make it slower and are unnecessary. (2) it will also cause /art/gallery/foo/bar/temp/test/whatever to also work. (3) a lot of duplicate content issues with this one. – Anthony Hatzopoulos Oct 26 '12 at 18:16
  • By all means, correct my answer :), it'll in turn provide a great answer for future visitors on this page. – Gerben Oct 26 '12 at 18:56
1

Yes, there is a way to do that. What you need is mod_rewrite installed and activated on your Apache server. Then create a .htaccess file, in which you can "tell" Apache which URLs to rewrite and specify the rules how to be done. So basically you'd need something like this

RewriteEngine On
RewriteBase /
RewriteRule ^gallery/([^/\.]+)/([^/\.]+)$ gallery.php?a=$1/$2 [L,NC]
Havelock
  • 6,913
  • 4
  • 34
  • 42
0

is this what your looking for?

RewriteEngine on
RewriteRule ^gallery/([^/\.]+) gallery.php?a=$1 [L]

or you could be more specific:

RewriteEngine on
RewriteRule ^gallery/([^/\.]+)/([^/\.]+) gallery.php?a=$1&b=$2 [L]
Samuel Cook
  • 16,620
  • 7
  • 50
  • 62