mod rewrite / remove query string AND add directory

All about creating websites!
Post Reply
User avatar
bad_brain
Site Owner
Site Owner
Posts: 11636
Joined: 06 Apr 2005, 16:00
19
Location: In your eye floaters.
Contact:

mod rewrite / remove query string AND add directory

Post by bad_brain »

phew...that was a hard one. we are using an updated image hosting script now for suck-o (check it out, it's sweet!), but sadly it is using a different way to handle the URLs.

on the old version, when uploading an image named 1.jpg the URL was:
/?v=1.jpg
but on the new one it would look something like:
/A

so I needed to rewrite the old URLs in order to keep the old images displayed, which took a lot of failed attempts (including nice results like redirect loops with hundreds of /images/images/images...), but I finally figured it out.

so here is how you can strip the query (?v= in this case) and add a directory to the URL:

Code: Select all

RewriteCond %{QUERY_STRING}  ^v=(.*)$  [NC]
RewriteRule ^ /images/%1? [NC,L,R=301]
so now it rewrites:
/?v=1.jpg
to:
/images/1.jpg

:)
Image

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: mod rewrite / remove query string AND add directory

Post by ayu »

nice :D
"The best place to hide a tree, is in a forest"

Post Reply