Forum Topic: Query strings redirect

Forum: .htaccess Forum : Redirecting • Posted by Paulo Vieira • Updated:

I want to redirect http://www.something.com/helloworld.pdf to http://www.something.com/index.php?file=helloworld.pdf and then echoing the name of the file in index.php.

So far I got this code:

RewriteEngine on
RewriteRule ([^/.]+)/?.(?i:jpg|gif|png|pdf|zip|rar|doc|docx|mov|ai|psd|swf|key)$ index.php?file=$1 [L]

Is working great but the only thing it?s missing is the extension of the file on the echo. What am I doing wrong?

3 Replies to “Query strings redirect”

Posted by Jeff Starr

Hey Paulo,

I’m not sure exactly, but can tell you that the $1 represents the first matched pattern in parentheses, ([^/.]+),.. so to get the file extension from the second parentheses, use $2 (because it’s the second pattern matched).

Posted by Paulo Vieira •

Let me reorder my question and explain what I want to accomplish.

Let’s say I have http://www.myhost.com/doc.pdf. I want to give the url http://www.myhost.com/client/doc.pdf to my client and when he enter that url http://www.myhost.com/index.php is called with the download option available and other info I want to show.

I changed the .htaccess code to this:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^(/assets/)
RewriteRule ^(.*)/(.*)$  index.php?client=$1&file=$2 [L]

And now is working great, the only thing is that I want to exclude assets from the redirection (I have index.php images, css and javascript there) and it’s not working.

Am I doing anything wrong?

Posted by Jeff Starr

It looks like it should work, although you may want to tweak the rewrite condition a bit:

RewriteCond %{REQUEST_URI} !^/assets/ [NC]

Things may also depend on the structure of the filesystem and relative location of the .htaccess file(s) being used.