Htaccess rewrites causing infinite loop - .htaccess

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

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

Related

Rewrite URL'S with a question mark “?”

I have a few url that have appeared in google search console with "?" and under scores "_"in the URL. I have tried many ways to redirect them but i have failed. I believe it has some thing to do with using the %{QUERY_STRING}
this is the URL that i need redirecting from
repairs-blog?journal_blog_tag=iPhone
to
blog?journal_blog_tag=iPhone
Maybe someone could write the solution?
When a query string is involved, you use RewriteCond with %{QUERY_STRING}, e.g.
RewriteCond %{QUERY_STRING} journal_blog_tag=
and then rewrite with RewriteRule
RewriteRule ^repairs-blog$ /blog [L]
Because you don't change the query string, there's nothing more to do.
When you want to rewrite this URL, no matter what query string is given, you can also omit the RewriteCond and just use the RewriteRule.
If you want to redirect instead of rewrite, e.g. change the URL in the client, you add the R flag
RewriteRule ^repairs-blog$ /blog [R,L]

ASCII Characters in Links Producing 404 Errors

I used to have a query string ?q= in all my urls. I now have these urls redirecting to the alias name.
RewriteRule ^(.*)$ index.php?q=$1 [L,NC]
RewriteCond %{QUERY_STRING} ^q=(.*)$ [NC]
RewriteRule ^$(.*) /%1? [R=301,L,NE]
Unfortunately other websites link to pages on my website in this manner www.example.com/%3Fq%3Dfaqs. I changed over all the links on my site to absolute but somehow google has indexed hundreds of pages that look like www.example.com/%3Fq%3Dfaqs?q=contact or www.example.com/%3Fq%3Dfaqs%3Fq%3Dcontact.
Is there a way I can rewrite these ASCII codes back to the symbols using htaccess or some other method?
I solved this by writing lines like Redirect 301 /?q=faqs example.com/?q=faqs This redirected /%3Fq%3Dfaqs to /?q=faqs which redirected to /faqs. Probably not the best way to do it, but it works.

Remove Last Character of URL with .htaccess

Somehow google managed to pickup some URLs on my site with a ) at the end which is causing a bunch of 404 links. The links look something like this:
"http://mysite.com/page.php?id=42523"
but what google has in its search results is:
"http://mysite.com/page.php?id=42523)"
Is there a way with .htaccess that I can determine if a url ends with ) and redirect it to the proper URL?
Try putting this in the htaccess file in your document root:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)\)$
RewriteRule ^(.*)$ /$1?%1 [L,R=301]

301 redirect from URL with query string to new domain with different query string

I am trying to figure out how to do a 301 redirect in my htaccess file to redirect some files to a new domain. Here's what I need to know how to do:
OLD URL: http://www.example.com/index.php?page=news&id=2366
NEW URL: http://www.example2.com/news.php?name=23546
The redirects don't have to be automatically created. I can hard-code the pages I need to redirect in the htaccess file, I just don't know the format to use as I've read that a "standard" 301 redirect won't work with query strings.
Basically this is what I want to do, but from my research so far it doesn't sound like it can be done this way.
redirect 301 /index.php?page=news&id=2366 http://www.example2.com/news.php?name=23546
You could use a rewrite rule with a query string match condition, such as:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/index.php$
RewriteCond %{QUERY_STRING} ^page=news&id=2366$
RewriteRule ^(.*)$ http://www.example2.com/news.php?name=23546 [R=301,L]
Checkout this blog page for more information on how this works.
I had the same problem, but still more complicated, because I needed to discard other parameters.
Like this: my-old-page.php?a=1&b=2&c=3
I need to use one of the strings and discard the others, but that solution only worked if I want to use the last parameter (c=3). If I want to use any other (a=1 or b=2) it runs to a 404. After much struggling and searching, I found an answer:
RewriteCond %{QUERY_STRING} ^.* ?b=2.* ?$ (without space after the *)
RewriteRule (.*) http://www.my-webpage.php/new-location-2? [R=301,L]
The solution is to add ".*?" before and after the parameter to use.
I don't know if this is the best solution, but it works.

301 Redirect via .htaccess php?i2=sajsak to productname.html - Not blanket

OK, so I'm trying to do a straight redirect:
store.php?StoreLevel=3&PrevStoreLevel=1&productid=6067&Level1=155&p=5&term=&BrowseBy=0
Redirected to:
mynewproductname.html
I'm not looking for a once size fits all rewrite rule, each one of these has a corresponding .html counter part, so kinda "hardcoded"
These are for 301's and Google Rankings
I can't figure this out. All resources I have found give me blanket rules on how to convert all php links in one shot to a html counterpart with the same name. I'm not looking to do this.
I've tried many things, none work. Some of my attempts:
RewriteRule ^store.php?StoreLevel=3&PrevStoreLevel=1&productid=6067&Level1=155&p=5&term=&BrowseBy=0 $ mynewproductname.html [R=301,L]
RewriteCond %{QUERY_STRING} =store.php?StoreLevel=3&PrevStoreLevel=1&productid=6067&Level1=155&p=5&term=&BrowseBy=0
RewriteRule mynewproductname.html [R=301,L]
RewriteCond %{QUERY_STRING} ^StoreLevel=3&PrevStoreLevel=1&productid=6067&Level1=155&p=5&term=&BrowseBy=0$
RewriteRule ^store.php$ http://mysite.com/mynewproductname.html? [R=301,L]
Some of these cause a 500 server error, some just don't work. Assistance is greatly appreciated.
EDIT - mod_rewrite is on and working fine, was able to redirect:
RewriteRule ^foo$ http://mysite.com/testing.html [R=301,L]
With success
Instead of using .htaccess, you could try redirecting from within your .php file. Have a lookup table of productid => webpage that gets scanned when the .php is first loaded; if you get a match, return a 301. If you don't match, continue on with regular .php processing (or 404, as appropriate)

Resources