1

We have a URL that needs to be redirected based on the values of two URL variables. The http request is as follows: http://img.ourserver.com/junk/imgfolder?var1=size&var2=imgname. This URL should redirect to the following http://img.ourserver.com/imgfolder/size/imgname.png

We can do this either in an .htaccess file or the Apache conf file.

Thanks!

Wandering Digital
  • 1,788
  • 2
  • 21
  • 27
  • 1
    So, what have you tried? You can do this using `RewriteRule` in your Apache configuration in combination with `RewriteCond`; there have been a number of questions here on SO in the past month or so that touch on this topic. – larsks May 04 '12 at 20:10
  • Our main problem is that this is effectively the opposite of a normal rewrite since our users will be providing the URL with vars, and we need to redirect to a clean directory structure. I haven't had much luck finding Q's that relate to this—especially with multiple variables on SA. – Wandering Digital May 04 '12 at 20:19
  • Well, [this](http://stackoverflow.com/questions/1244881/rewrite-query-string), for example, shows how to match arbitrary chunks of text in the query string and then substitute them into the target of a `RewriteRule`. This example only extracts a single chunk of text from the query string, but you can obviously generalize the solution. – larsks May 04 '12 at 20:20

1 Answers1

1

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} (^|&)var1=([^&]+)&var2=([^&]+) [NC]
RewriteRule ^[^/]+/(imgfolder)/?$ $1/%1/%2.png? [L,R,NC]
anubhava
  • 761,203
  • 64
  • 569
  • 643