Loai Mostafa
Loai Mostafa

Reputation: 37

link redirection using htaccess RewriteRule

I have these links in my website:

www.example.org/folder/files.php?file=folder/document.pdf

www.example.org/folder/files.php?force&file=2009.pdf


and I want redirect to :

www.example.org/files/folder/document.pdf

www.example.org/files/2009.pdf

I tried :

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^files/(.*)$ /files.php?file=$1 [R=301,L]  
</IfModule>

but doesn't work! any help?

Upvotes: 0

Views: 128

Answers (2)

David Ravetti
David Ravetti

Reputation: 2040

RewriteRule ^files/(.*)$ /files.php?file=$1 [R=301,L]

There are two issues with this rule ... first, what you are matching needs to appear first in the rule, then what you are rewriting appears second - you have that backwards.

Once you reverse that, though, you run into the second issue - you can't match query strings in a RewriteRule, you need to match them in a RewriteCond:

To match www.example.org/folder/files.php?force&file=2009.pdf and redirect it to www.example.org/files/2009.pdf you would do:

RewriteCond %{QUERY_STRING} ^force&file=(.*)$ [NC]
RewriteRule ^folder/files.php$ /files/%1 [R=301, L]

The %1 matches what's in the parentheses in the RewriteCond.

Upvotes: 1

Ishpreet
Ishpreet

Reputation: 169

Search on google first. The first thing displayed on google for htaccess is htaccess redirect. I think Redirect /olddirectory/oldfile.html http://example.com/newdirectory/newfile.html (same line with a space) should work. Go to http://kb.mediatemple.net/questions/242/How+do+I+redirect+my+site+using+a+.htaccess+file%3F . Php would also do the work. Just goolgle things before asking them.

Upvotes: 0

Related Questions