Kravitz
Kravitz

Reputation: 2859

Htaccess rewrites causing infinite loop

I am trying to write a redirect for a PDF file in my wordpress install that has shown up in my google webmaster tools with a load of query strings after it, what I want is to have the redirect match any number or query stings after the pdf filename and just redirect directly to the file.

     RewriteRule ^(.*) http://www.example.com/wp-content/uploads/2014/08/test\.pdf$ [R=301,L]

and this also (as well as numerous different iterations)

    RedirectMatch 301   ^/wp-content/uploads/2014/08/test\.pdf(.*)$ /wp-content/uploads/2014/08/test.pdf?

However they either result in a continuous redirect loop, 404 error, or still leave part of the query string

the url has any number of random query strings so something that would catch anything after the filename and just redirect to the bare file is percisely what I am looking for. here is an example of the url:

http://www.example.com/wp-content/uploads/2014/08/test.pdf?test=324234234.ffdsfewrgfdgdfg.234234.234324.2333333&fsdf=3432423.1.34234324&wer=werewrewr

or

http://www.example.com/wp-content/uploads/2014/08/test.pdf?test=324234234.ffdsfewrgfdgdfg.234234.234324.2333333&fsdf=3432423.1.34234324&wer=werewrewr&fsdf=3432423.1.34234324&wer=werewrewr&fsdf=3432423.1.34234324&wer=werewrewr

and what I want to get is just

http://www.example.com/wp-content/uploads/2014/08/test.pdf

Thanks

Upvotes: 0

Views: 40

Answers (1)

Panama Jack
Panama Jack

Reputation: 24448

Try something like this with mod_rewrite.

RewriteEngine On
RewriteCond %{QUERY_STRING} .
RewriteCond %{REQUEST_URI} (.+)\.pdf$
RewriteRule ^(.*) /$1? [R=301,L]

Upvotes: 1

Related Questions