Query string in htaccess rule doesn't work - .htaccess

I've a problem with the rule below:
RewriteCond %{QUERY_STRING} ^p=([0-9]+)
RewriteRule ^[ROOT]/([0-9]+)_(.*)$ https://www.[DOMAIN].[EXT]/[ROOT]/$1-$2?page=([0-9]+) [R=301,L]
I need to redirect the old qs parameter (p) to the new one (page), but the result is this:
https://www.[DOMAIN].[EXT]/[ROOT]/[ID]-[STRING]?page=([0-9]+)
I mean: if I've ?p=4 I need to go to ?page=4
I don't understand what's wrong
Thank you all

Related

Htaccess 301 redirect with query string params

This is so elementary that you have to ask why it is so complex to implement.
Simply need to redirect one url to another on the same domain, the url contains query string aparams though:
I tried:
RewriteRule ^article.php?section=exclusive&id=543 articles/view/4639 [R=301,L]
I get page cannot be found - the redirect is not happening. I have other re-directs in the same htaccess which work. The query string params are causing problems, and escaping them also does not help.
Note the id's of the two urls are not the same and therefore I don't want a clever re-write rule which converts from one url to another with the query strings mapped to new params. I just want a fast and dirty mapping from A to B.
UPDATE
I tried this it redirects but it is adding my old query string to the end of the new url. How can I prevent that?
RewriteCond %{REQUEST_URI} ^/article\.php$
RewriteCond %{QUERY_STRING} ^section=exclusive
RewriteRule ^$ http://www.onlinegooner.com/articles/view/3000 [R=301,L]
Input url is: mydomain.com/article.php?section=exclusive
You may use this rule:
RewriteCond %{QUERY_STRING} ^section=exclusive [NC]
RewriteRule ^article\.php$ /articles/view/3000? [R=301,L,NC]
? in the target will strip off previous query string.

rewrite rule, no clue

I would like to redirect
pins?board=10000011
to
view_photos.php?album=10000011
10000011 being an id that is dynamic, therefore the no clue in the title of this question, do not know how to grab that and use inside a rewrite rule.
have tried with:
RewriteRule ^([A-Za-z-0-9-_.]+)/pins?board= $1/view_photos.php?album=$2
it seems I obviously don't know exactly what I'm doing so I'm asking for help.
You can't match against the query string in a rewrite rule. You need to use the %{QUERY_STRING} var in a rewrite condition:
RewriteCond %{QUERY_STRING} ^board=(.*)$
RewriteRule ^/?([A-Za-z-0-9-_.]+)/pins$ /$1/view_photos.php?album=%1 [L,R=301]

htaccess redirect specific url to another domain

I want to redirect specific URL with params to another domain
http://domain.tld/buy/?w=X1234 to http://anotherdomain.tld/product.php?id=X1234
Here my htaccess
RewriteCond %{REQUEST_URI} !^/*buy/(.*)$ [NC]
RewriteRule ^/*buy/?w=([a-zA-Z0-9]+)$ http://anotherdomain.tld/product.php?id=$1 [NC,L,R=301]
but not working. maybe the problem is param ?w=
Thank you
UPDATE: i solve the problem by using Query string, after reading How to REGEX and .htaccess rewrite url
RewriteCond %{QUERY_STRING} ^w=([a-zA-Z0-9]+)$
RewriteRule ^/*buy/$ http://anotherdomain.tld/product.php?id=%1 [NC,L,R=301]
Thank you stackoverflow and folks! i m start understanding regex from here ^_^
That's right, your params won't be picked up, but they can be passed on.
Something like;
RewriteRule ^buy/([a-zA-Z0-9]+)$ http://anotherdomain.tld/product.php?id=$1 [NC,L,R=301]
Where your original url would be; /buy/X1234

Rewrite trouble in htaccess

So I have a kind of strange trouble. I have a rewrite rule from
mysite.com/brand.php?brand=example&ref=ex1
to
mysite.com/brand/example/ex1
And I managed to do that, but there is also another problem: when you type this:
mysite.com/brand/example/ex1.html
or
mysite.com/brand/example/ex1.php
I get this:
mysite.com/brand/example/ex1?ref=ex1.php&brand=example
Can anyone tell why I get this redirect and how to get rid of it?
I tried this:
RedirectMatch ^brand/([A-Za-z0-9-\.\_]+)/([A-Za-z0-9-\.\_]+)php$ /brand/$1/$2
or this:
RedirectMatch ^brand/([A-Za-z0-9-\.\_]+)/([A-Za-z0-9-\.\_]+)\.php$ /brand/$1/$2
but it simply doesn't work.
Please, anyone help me!
I would use the mod_rewrite for this:
RewriteEngine On
RewriteCond %{REQUEST_URI} =/brand.php
RewriteCond %{QUERY_STRING} ^brand=([^&]+&ref=(.+)$
RewriteRule .* /brand/%1/%2? [L,R]
The RewriteRule is applied only when both RewriteCond are valid. The first RewriteCond compares the request URL to be /brand.php, the second matches regular expression against the query string and stores the matches in %1 and %2 respectively.
In the RewriteRule is the URL rewritten using the matched parts from query string.

htaccess rewrite querystring and remove empty value

first, sorry for my bad English.
I try to rewrite url generated from Form Get and redirect that.
my url is like this:
http://www.mysite.com/properties?action=search&agreement=for-rent&category=my-category&type=&zone=my-zone&city=my-city
and I have this .htaccess configured:
11. RewriteCond %{QUERY_STRING} ^action=(?:[a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)$
12. RewriteRule (.*) %{REQUEST_URI}/%1/%2/%3/%4/%5/? [R=301,L]
So basically all my request are direct to index.php.
21. RewriteCond %{REQUEST_URI} !index\.php|resources|hidden
22. RewriteRule ^(.*)$ index.php/$1 [L]
All works, but the problem is when I have an empty value in query string, the rule add double slash and the above url (for example whit &type=&zone=my-zone... type have empty value) will translate like that:
http://www.mysite.com/for-rent/my-category//my-zone/my-city/
The question is: How can i remove in .htaccess the double slash generated if i have one or more empty value in query string?
Thanks
Easiest is to do another redirect (not real pretty as it requires two 301's).
RewriteCond %{THE_REQUEST} //
RewriteRule .* $0 [R=301,L]
The fun part is that when the url is loaded with a double slash in it, mod_rewrite will automatically remove this. So as you can see above you'll just have to rewrite the url to itself, kind of.

Resources